From e58fabd89f9708aaeb10e52f731759712034833f Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Mon, 18 May 2020 18:02:20 +0200 Subject: WIP: Refactoring --- pkg/libgemini/libgemini.go | 64 ++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 28 deletions(-) (limited to 'pkg') 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 ( HeaderPattern = regexp.MustCompile("^(\\d\\d)[ \\t]+(.*)$") LinkPattern = regexp.MustCompile("^=>[ \\t]*([^ \\t]+)(?:[ \\t]+(.*))?$") ReflowModePattern = regexp.MustCompile("^```(.*)$") - Heading1Pattern = regexp.MustCompile("^#(.*)$") - Heading2Pattern = regexp.MustCompile("^##(.*)$") - Heading3Pattern = regexp.MustCompile("^###(.*)$") - ListItemPattern = regexp.MustCompile("^\\*(.*)$") + Heading1Pattern = regexp.MustCompile("^# *(.*)$") + Heading2Pattern = regexp.MustCompile("^## *(.*)$") + Heading3Pattern = regexp.MustCompile("^### *(.*)$") + ListItemPattern = regexp.MustCompile("^\\* *(.*)$") TermEscapeSGRPattern = regexp.MustCompile("\\[\\d+(;\\d+)*m") ) @@ -82,6 +82,27 @@ const ( LIST = GeminiDocSectionType(6) ) +func (it GeminiDocSectionType) String() string { + switch it { + case RAW_TEXT: + return "RAW_TEXT" + case REFLOW_TEXT: + return "REFLOW_TEXT" + case LINK: + return "LINK" + case HEADING_1: + return "HEADING_1" + case HEADING_2: + return "HEADING_2" + case HEADING_3: + return "HEADING_3" + case LIST: + return "LIST" + default: + return "???" + } +} + type GeminiDocSection struct { Type GeminiDocSectionType Text string @@ -191,20 +212,13 @@ func ParseGeminiDocument(body *bytes.Buffer) (sections []GeminiDocSection) { if !reflow { if !ignoreSection { - if section.Type != RAW_TEXT { - sections = append(sections, section) - section = GeminiDocSection{ - Type: RAW_TEXT, - } - } - } else { - ignoreSection = false - section = GeminiDocSection{ - Type: RAW_TEXT, - } + sections = append(sections, section) } - section.Text = section.Text + "\n" + line + section = GeminiDocSection{ + Type: RAW_TEXT, + Text: line, + } continue } @@ -297,20 +311,14 @@ func ParseGeminiDocument(body *bytes.Buffer) (sections []GeminiDocSection) { } if !ignoreSection { - if section.Type != REFLOW_TEXT { - sections = append(sections, section) - section = GeminiDocSection{ - Type: REFLOW_TEXT, - } - } - } else { - ignoreSection = false - section = GeminiDocSection{ - Type: REFLOW_TEXT, - } + sections = append(sections, section) } - section.Text = section.Text + "\n" + line + ignoreSection = false + section = GeminiDocSection{ + Type: REFLOW_TEXT, + Text: line, + } } if !ignoreSection { -- cgit v1.2.3-70-g09d2