2020-10-30 13:25:48 +00:00
|
|
|
package markup
|
2020-08-05 15:08:59 +00:00
|
|
|
|
|
|
|
import ()
|
|
|
|
|
|
|
|
const maxRecursionLevel = 3
|
|
|
|
|
|
|
|
type GemParserState struct {
|
|
|
|
recursionLevel int
|
|
|
|
}
|
|
|
|
|
|
|
|
func Parse(ast []Line, from, to int, state GemParserState) (html string) {
|
|
|
|
if state.recursionLevel > maxRecursionLevel {
|
|
|
|
return "Transclusion depth limit"
|
|
|
|
}
|
|
|
|
for _, line := range ast {
|
2020-11-03 12:27:26 +00:00
|
|
|
if line.id >= from && (line.id <= to || to == 0) || line.id == -1 {
|
2020-08-05 15:08:59 +00:00
|
|
|
switch v := line.contents.(type) {
|
|
|
|
case Transclusion:
|
|
|
|
html += Transclude(v, state)
|
2020-11-03 15:41:50 +00:00
|
|
|
case Img:
|
|
|
|
html += v.ToHtml()
|
2020-08-05 15:08:59 +00:00
|
|
|
case string:
|
|
|
|
html += v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return html
|
|
|
|
}
|
|
|
|
|
|
|
|
func ToHtml(name, text string) string {
|
|
|
|
state := GemParserState{}
|
|
|
|
return Parse(lex(name, text), 0, 0, state)
|
|
|
|
}
|