1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 11:46:16 +00:00
mycorrhiza/markup/lexer.go

182 lines
4.5 KiB
Go
Raw Normal View History

2020-10-30 13:25:48 +00:00
package markup
import (
"fmt"
"html"
"strings"
)
// HyphaExists holds function that checks that a hypha is present.
var HyphaExists func(string) bool
// HyphaAccess holds function that accesses a hypha by its name.
var HyphaAccess func(string) (rawText, binaryHtml string, err error)
2020-10-30 13:25:48 +00:00
// GemLexerState is used by markup parser to remember what is going on.
type GemLexerState struct {
// Name of hypha being parsed
name string
where string // "", "list", "pre"
// Line id
id int
buf string
2020-11-03 15:41:50 +00:00
// Temporaries
img Img
}
type Line struct {
id int
// interface{} may be bad. What I need is a sum of string and Transclusion
contents interface{}
}
func lex(name, content string) (ast []Line) {
var state = GemLexerState{name: name}
2020-08-28 19:17:32 +00:00
for _, line := range append(strings.Split(content, "\n"), "") {
geminiLineToAST(line, &state, &ast)
}
return ast
}
2020-10-30 13:25:48 +00:00
// Lex `line` in markup and save it to `ast` using `state`.
func geminiLineToAST(line string, state *GemLexerState, ast *[]Line) {
2020-08-28 19:17:32 +00:00
addLine := func(text interface{}) {
*ast = append(*ast, Line{id: state.id, contents: text})
}
if "" == strings.TrimSpace(line) {
if state.where == "list" {
2020-08-28 19:17:32 +00:00
state.where = ""
addLine(state.buf + "</ul>")
} else if state.where == "number" {
state.where = ""
addLine(state.buf + "</ol>")
2020-08-28 19:17:32 +00:00
}
return
}
startsWith := func(token string) bool {
return strings.HasPrefix(line, token)
}
// Beware! Usage of goto. Some may say it is considered evil but in this case it helped to make a better-structured code.
switch state.where {
2020-11-03 15:41:50 +00:00
case "img":
goto imgState
case "pre":
goto preformattedState
case "list":
goto listState
2020-11-03 15:47:22 +00:00
case "number":
goto numberState
default:
goto normalState
}
2020-11-03 15:41:50 +00:00
imgState:
if shouldGoBackToNormal := state.img.Process(line); shouldGoBackToNormal {
state.where = ""
addLine(state.img)
}
return
preformattedState:
switch {
case startsWith("```"):
state.where = ""
state.buf = strings.TrimSuffix(state.buf, "\n")
addLine(state.buf + "</code></pre>")
state.buf = ""
default:
state.buf += html.EscapeString(line) + "\n"
}
return
listState:
switch {
case startsWith("* "):
2020-11-04 17:42:02 +00:00
state.buf += fmt.Sprintf("\t<li>%s</li>\n", ParagraphToHtml(state.name, line[2:]))
case startsWith("```"):
state.where = "pre"
addLine(state.buf + "</ul>")
state.id++
state.buf = fmt.Sprintf("<pre id='%d' alt='%s' class='codeblock'><code>", state.id, strings.TrimPrefix(line, "```"))
default:
state.where = ""
addLine(state.buf + "</ul>")
goto normalState
}
return
2020-11-03 15:47:22 +00:00
numberState:
switch {
case startsWith("*. "):
2020-11-04 17:42:02 +00:00
state.buf += fmt.Sprintf("\t<li>%s</li>\n", ParagraphToHtml(state.name, line[3:]))
2020-11-03 15:47:22 +00:00
case startsWith("```"):
state.where = "pre"
addLine(state.buf + "</ol>")
state.id++
state.buf = fmt.Sprintf("<pre id='%d' alt='%s' class='codeblock'><code>", state.id, strings.TrimPrefix(line, "```"))
default:
state.where = ""
addLine(state.buf + "</ol>")
goto normalState
}
return
normalState:
state.id++
switch {
case startsWith("```"):
state.where = "pre"
state.buf = fmt.Sprintf("<pre id='%d' alt='%s' class='codeblock'><code>", state.id, strings.TrimPrefix(line, "```"))
2020-10-30 13:25:48 +00:00
case startsWith("* "):
state.where = "list"
state.buf = fmt.Sprintf("<ul id='%d'>\n", state.id)
goto listState
2020-11-03 15:47:22 +00:00
case startsWith("*. "):
state.where = "number"
state.buf = fmt.Sprintf("<ol id='%d'>\n", state.id)
goto numberState
2020-10-30 13:25:48 +00:00
case startsWith("###### "):
addLine(fmt.Sprintf(
"<h6 id='%d'>%s</h6>", state.id, line[7:]))
case startsWith("##### "):
addLine(fmt.Sprintf(
"<h5 id='%d'>%s</h5>", state.id, line[6:]))
case startsWith("#### "):
addLine(fmt.Sprintf(
"<h4 id='%d'>%s</h4>", state.id, line[5:]))
case startsWith("### "):
addLine(fmt.Sprintf(
2020-10-30 13:25:48 +00:00
"<h3 id='%d'>%s</h3>", state.id, line[4:]))
case startsWith("## "):
addLine(fmt.Sprintf(
2020-10-30 13:25:48 +00:00
"<h2 id='%d'>%s</h2>", state.id, line[3:]))
case startsWith("# "):
addLine(fmt.Sprintf(
2020-10-30 13:25:48 +00:00
"<h1 id='%d'>%s</h1>", state.id, line[2:]))
case startsWith(">"):
addLine(fmt.Sprintf(
"<blockquote id='%d'>%s</blockquote>", state.id, remover(">")(line)))
case startsWith("=>"):
2020-11-04 17:42:02 +00:00
href, text, class := Rocketlink(line, state.name)
addLine(fmt.Sprintf(
2020-11-04 17:42:02 +00:00
`<p><a id='%d' class='rocketlink %s' href="%s">%s</a></p>`, state.id, class, href, text))
case startsWith("<="):
addLine(parseTransclusion(line, state.name))
case line == "----":
*ast = append(*ast, Line{id: -1, contents: "<hr/>"})
2020-11-03 15:41:50 +00:00
case MatchesImg(line):
state.where = "img"
state.img = ImgFromFirstLine(line, state.name)
default:
2020-11-04 17:42:02 +00:00
addLine(fmt.Sprintf("<p id='%d'>%s</p>", state.id, ParagraphToHtml(state.name, line)))
}
}