diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/libgemini/libgemini.go | 64 |
1 files changed, 36 insertions, 28 deletions
diff --git a/pkg/libgemini/libgemini.go b/pkg/libgemini/libgemini.go index 48a8ed0..5e37490 100644 --- a/pkg/libgemini/libgemini.go +++ b/pkg/libgemini/libgemini.go | |||
@@ -53,10 +53,10 @@ var ( | |||
53 | HeaderPattern = regexp.MustCompile("^(\\d\\d)[ \\t]+(.*)$") | 53 | HeaderPattern = regexp.MustCompile("^(\\d\\d)[ \\t]+(.*)$") |
54 | LinkPattern = regexp.MustCompile("^=>[ \\t]*([^ \\t]+)(?:[ \\t]+(.*))?$") | 54 | LinkPattern = regexp.MustCompile("^=>[ \\t]*([^ \\t]+)(?:[ \\t]+(.*))?$") |
55 | ReflowModePattern = regexp.MustCompile("^```(.*)$") | 55 | ReflowModePattern = regexp.MustCompile("^```(.*)$") |
56 | Heading1Pattern = regexp.MustCompile("^#(.*)$") | 56 | Heading1Pattern = regexp.MustCompile("^# *(.*)$") |
57 | Heading2Pattern = regexp.MustCompile("^##(.*)$") | 57 | Heading2Pattern = regexp.MustCompile("^## *(.*)$") |
58 | Heading3Pattern = regexp.MustCompile("^###(.*)$") | 58 | Heading3Pattern = regexp.MustCompile("^### *(.*)$") |
59 | ListItemPattern = regexp.MustCompile("^\\*(.*)$") | 59 | ListItemPattern = regexp.MustCompile("^\\* *(.*)$") |
60 | TermEscapeSGRPattern = regexp.MustCompile("\\[\\d+(;\\d+)*m") | 60 | TermEscapeSGRPattern = regexp.MustCompile("\\[\\d+(;\\d+)*m") |
61 | ) | 61 | ) |
62 | 62 | ||
@@ -82,6 +82,27 @@ const ( | |||
82 | LIST = GeminiDocSectionType(6) | 82 | LIST = GeminiDocSectionType(6) |
83 | ) | 83 | ) |
84 | 84 | ||
85 | func (it GeminiDocSectionType) String() string { | ||
86 | switch it { | ||
87 | case RAW_TEXT: | ||
88 | return "RAW_TEXT" | ||
89 | case REFLOW_TEXT: | ||
90 | return "REFLOW_TEXT" | ||
91 | case LINK: | ||
92 | return "LINK" | ||
93 | case HEADING_1: | ||
94 | return "HEADING_1" | ||
95 | case HEADING_2: | ||
96 | return "HEADING_2" | ||
97 | case HEADING_3: | ||
98 | return "HEADING_3" | ||
99 | case LIST: | ||
100 | return "LIST" | ||
101 | default: | ||
102 | return "???" | ||
103 | } | ||
104 | } | ||
105 | |||
85 | type GeminiDocSection struct { | 106 | type GeminiDocSection struct { |
86 | Type GeminiDocSectionType | 107 | Type GeminiDocSectionType |
87 | Text string | 108 | Text string |
@@ -191,20 +212,13 @@ func ParseGeminiDocument(body *bytes.Buffer) (sections []GeminiDocSection) { | |||
191 | 212 | ||
192 | if !reflow { | 213 | if !reflow { |
193 | if !ignoreSection { | 214 | if !ignoreSection { |
194 | if section.Type != RAW_TEXT { | 215 | sections = append(sections, section) |
195 | sections = append(sections, section) | ||
196 | section = GeminiDocSection{ | ||
197 | Type: RAW_TEXT, | ||
198 | } | ||
199 | } | ||
200 | } else { | ||
201 | ignoreSection = false | ||
202 | section = GeminiDocSection{ | ||
203 | Type: RAW_TEXT, | ||
204 | } | ||
205 | } | 216 | } |
206 | 217 | ||
207 | section.Text = section.Text + "\n" + line | 218 | section = GeminiDocSection{ |
219 | Type: RAW_TEXT, | ||
220 | Text: line, | ||
221 | } | ||
208 | 222 | ||
209 | continue | 223 | continue |
210 | } | 224 | } |
@@ -297,20 +311,14 @@ func ParseGeminiDocument(body *bytes.Buffer) (sections []GeminiDocSection) { | |||
297 | } | 311 | } |
298 | 312 | ||
299 | if !ignoreSection { | 313 | if !ignoreSection { |
300 | if section.Type != REFLOW_TEXT { | 314 | sections = append(sections, section) |
301 | sections = append(sections, section) | ||
302 | section = GeminiDocSection{ | ||
303 | Type: REFLOW_TEXT, | ||
304 | } | ||
305 | } | ||
306 | } else { | ||
307 | ignoreSection = false | ||
308 | section = GeminiDocSection{ | ||
309 | Type: REFLOW_TEXT, | ||
310 | } | ||
311 | } | 315 | } |
312 | 316 | ||
313 | section.Text = section.Text + "\n" + line | 317 | ignoreSection = false |
318 | section = GeminiDocSection{ | ||
319 | Type: REFLOW_TEXT, | ||
320 | Text: line, | ||
321 | } | ||
314 | } | 322 | } |
315 | 323 | ||
316 | if !ignoreSection { | 324 | if !ignoreSection { |