From ac770231436f8a17d348a6a0ab934429df3c57d0 Mon Sep 17 00:00:00 2001 From: Feuerfuchs Date: Mon, 18 May 2020 16:12:43 +0200 Subject: WIP: Refactoring --- pkg/libgemini/libgemini.go | 85 +++++++++++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 35 deletions(-) (limited to 'pkg/libgemini/libgemini.go') diff --git a/pkg/libgemini/libgemini.go b/pkg/libgemini/libgemini.go index 71012ef..48a8ed0 100644 --- a/pkg/libgemini/libgemini.go +++ b/pkg/libgemini/libgemini.go @@ -6,7 +6,6 @@ import ( "crypto/tls" "errors" "fmt" - "html/template" "io" "mime" "net" @@ -74,19 +73,19 @@ type Response struct { type GeminiDocSectionType byte const ( - RAW_TEXT = SectionType(0) - REFLOW_TEXT = SectionType(1) - LINK = SectionType(2) - HEADING_1 = SectionType(3) - HEADING_2 = SectionType(4) - HEADING_3 = SectionType(5) - LIST = SectionType(6) + RAW_TEXT = GeminiDocSectionType(0) + REFLOW_TEXT = GeminiDocSectionType(1) + LINK = GeminiDocSectionType(2) + HEADING_1 = GeminiDocSectionType(3) + HEADING_2 = GeminiDocSectionType(4) + HEADING_3 = GeminiDocSectionType(5) + LIST = GeminiDocSectionType(6) ) type GeminiDocSection struct { - Type SectionType + Type GeminiDocSectionType Text string - URL template.URL + URL string Items []string } @@ -171,13 +170,13 @@ func ParseHeader(line string) (header *Header, err error) { return } -func ParseGeminiDocument(body *bytes.Buffer) (sections []Section) { +func ParseGeminiDocument(body *bytes.Buffer) (sections []GeminiDocSection) { scanner := bufio.NewScanner(body) reflow := true ignoreSection := true - section := Section{ - Type: REFLOW_TEXT + section := GeminiDocSection{ + Type: REFLOW_TEXT, } for scanner.Scan() { @@ -185,23 +184,23 @@ func ParseGeminiDocument(body *bytes.Buffer) (sections []Section) { line = TermEscapeSGRPattern.ReplaceAllString(line, "") reflowMatch := ReflowModePattern.FindStringSubmatch(line) - if len(heading3Match) != 0 { + if len(reflowMatch) != 0 && reflowMatch[0] != "" { reflow = !reflow continue } if !reflow { if !ignoreSection { - if section.Type != REFLOW_TEXT { + if section.Type != RAW_TEXT { sections = append(sections, section) - section = Section{ - Type: REFLOW_TEXT + section = GeminiDocSection{ + Type: RAW_TEXT, } } } else { ignoreSection = false - section = Section{ - Type: REFLOW_TEXT + section = GeminiDocSection{ + Type: RAW_TEXT, } } @@ -222,80 +221,96 @@ func ParseGeminiDocument(body *bytes.Buffer) (sections []Section) { } ignoreSection = false - section = Section{ + section = GeminiDocSection{ Type: LINK, Text: label, - URL: template.URL(resolveURI(linkMatch[1], baseURL)), + URL: linkMatch[1], } continue } heading3Match := Heading3Pattern.FindStringSubmatch(line) - if len(heading3Match) != 0 { + if len(heading3Match) != 0 && heading3Match[0] != "" { if !ignoreSection { sections = append(sections, section) } ignoreSection = false - section = Section{ + section = GeminiDocSection{ Type: HEADING_3, - Text: heading3Match[1] + Text: heading3Match[1], } continue } heading2Match := Heading2Pattern.FindStringSubmatch(line) - if len(heading2Match) != 0 { + if len(heading2Match) != 0 && heading2Match[0] != "" { if !ignoreSection { sections = append(sections, section) } ignoreSection = false - section = Section{ + section = GeminiDocSection{ Type: HEADING_2, - Text: heading2Match[1] + Text: heading2Match[1], } continue } heading1Match := Heading1Pattern.FindStringSubmatch(line) - if len(heading1Match) != 0 { + if len(heading1Match) != 0 && heading1Match[0] != "" { if !ignoreSection { sections = append(sections, section) } ignoreSection = false - section = Section{ + section = GeminiDocSection{ Type: HEADING_1, - Text: heading1Match[1] + Text: heading1Match[1], } continue } listItemMatch := ListItemPattern.FindStringSubmatch(line) - if len(listItemMatch) != 0 { + if len(listItemMatch) != 0 && listItemMatch[0] != "" { if !ignoreSection { if section.Type != LIST { sections = append(sections, section) - section = Section{ - Type: LIST + section = GeminiDocSection{ + Type: LIST, } } } else { ignoreSection = false - section = Section{ + section = GeminiDocSection{ Type: LIST, } } - section.Items = append(section.Items, listItemMatch[1]) + section.Items = append(section.Items, listItemMatch[1]) continue } + + if !ignoreSection { + if section.Type != REFLOW_TEXT { + sections = append(sections, section) + section = GeminiDocSection{ + Type: REFLOW_TEXT, + } + } + } else { + ignoreSection = false + section = GeminiDocSection{ + Type: REFLOW_TEXT, + } + } + + section.Text = section.Text + "\n" + line } if !ignoreSection { -- cgit v1.2.3-54-g00ecf