oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package notionmd
2
3import (
4 "time"
5
6 "amadan.net/oficina/mapa/internal/mapa/markup"
7)
8
9// Dates (§11.2): a day is a date link, P4. A time follows its day's
10// link, in the time zone Notion gives, or the team's when it gives
11// none. A range is two of them with an en dash between.
12
13// timeLayouts are Notion's date-time forms without an offset, which it
14// sends when it names a time zone.
15var timeLayouts = []string{"2006-01-02T15:04:05.000", "2006-01-02T15:04:05",
16 "2006-01-02T15:04"}
17
18// date maps a date mention, or its text when Notion's value is not one
19// this can read.
20func (mapper *mapper) date(mention Mention, text string) []part {
21 zone := mapper.zone(mention.TimeZone)
22 start, ok := when(mention.Start, zone)
23 if !ok {
24 return []part{lit(text)}
25 }
26 if mention.End == "" {
27 return start
28 }
29 end, ok := when(mention.End, zone)
30 if !ok {
31 return start
32 }
33 return append(append(start, lit(" – ")), end...)
34}
35
36// zone is the mention's own time zone, else the team's, else UTC.
37func (mapper *mapper) zone(name string) *time.Location {
38 if name != "" {
39 if location, err := time.LoadLocation(name); err == nil {
40 return location
41 }
42 }
43 if mapper.options.Zone != nil {
44 return mapper.options.Zone
45 }
46 return time.UTC
47}
48
49// when is one date's parts: its day's link, then its time if it has
50// one.
51func when(value string, zone *time.Location) ([]part, bool) {
52 if markup.IsDate(value) {
53 return []part{dateLink(value)}, true
54 }
55 moment, ok := parseTime(value, zone)
56 if !ok {
57 return nil, false
58 }
59 local := moment.In(zone)
60 return []part{dateLink(local.Format(time.DateOnly)),
61 lit(" " + local.Format("15:04"))}, true
62}
63
64func parseTime(value string, zone *time.Location) (time.Time, bool) {
65 if moment, err := time.Parse(time.RFC3339Nano, value); err == nil {
66 return moment, true
67 }
68 for _, layout := range timeLayouts {
69 moment, err := time.ParseInLocation(layout, value, zone)
70 if err == nil {
71 return moment, true
72 }
73 }
74 return time.Time{}, false
75}
76
77func dateLink(date string) part { return part{kind: wiki, target: date} }
78