summaryrefslogtreecommitdiff
path: root/gemini/gemtext/parse.go
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-01-17 16:41:04 -0700
committertjpcc <tjp@ctrl-c.club>2023-01-17 16:41:04 -0700
commit6586db782ea6dcb5f2eb191a690ec7e7df51161f (patch)
tree36158a53a6d8aad9f5a873c6c43d598ce5647b97 /gemini/gemtext/parse.go
parent2ef530daa47b301a40c1ee93cd43b8f36fc68c0b (diff)
Updates
* update README * move "gemtext" to within "gemini"
Diffstat (limited to 'gemini/gemtext/parse.go')
-rw-r--r--gemini/gemtext/parse.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/gemini/gemtext/parse.go b/gemini/gemtext/parse.go
new file mode 100644
index 0000000..7041fde
--- /dev/null
+++ b/gemini/gemtext/parse.go
@@ -0,0 +1,49 @@
+package gemtext
+
+import (
+ "bufio"
+ "io"
+)
+
+// Parse parses the full contents of an io.Reader into a gemtext.Document.
+func Parse(input io.Reader) (Document, error) {
+ rdr := bufio.NewReader(input)
+
+ var lines []Line
+ inPFT := false
+
+ for {
+ raw, err := rdr.ReadBytes('\n')
+ if err != io.EOF && err != nil {
+ return nil, err
+ }
+
+ var line Line
+
+ if inPFT && (len(raw) < 3 || raw[0] != '`' || raw[1] != '`' || raw[2] != '`') {
+ line = PreformattedTextLine{raw: raw}
+ } else {
+ line = ParseLine(raw)
+ }
+
+ if line != nil && line.Type() == LineTypePreformatToggle {
+ if inPFT {
+ toggle := line.(PreformatToggleLine)
+ (&toggle).clearAlt()
+ line = toggle
+ }
+
+ inPFT = !inPFT
+ }
+
+ if line != nil {
+ lines = append(lines, line)
+ }
+
+ if err == io.EOF {
+ break
+ }
+ }
+
+ return Document(lines), nil
+}