From 987dc5b20ff984e254114ebb13fc0cbbe7b040ae Mon Sep 17 00:00:00 2001 From: bouncepaw Date: Mon, 4 Jan 2021 01:39:54 +0500 Subject: [PATCH] Support consecutive lines in paragraphs --- markup/lexer.go | 32 ++++++++++++++++++++++++++++++-- metarrhiza | 2 +- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/markup/lexer.go b/markup/lexer.go index b69a3cf..4dae95b 100644 --- a/markup/lexer.go +++ b/markup/lexer.go @@ -51,6 +51,13 @@ func lineToAST(line string, state *GemLexerState, ast *[]Line) { addLine := func(text interface{}) { *ast = append(*ast, Line{id: state.id, contents: text}) } + addParagraphIfNeeded := func() { + if state.where == "p" { + state.where = "" + addLine(fmt.Sprintf("

%s

", state.id, strings.ReplaceAll(ParagraphToHtml(state.name, state.buf), "\n", "
"))) + state.buf = "" + } + } // Process empty lines depending on the current state if "" == strings.TrimSpace(line) { @@ -66,6 +73,8 @@ func lineToAST(line string, state *GemLexerState, ast *[]Line) { case "launchpad": state.where = "" addLine(state.buf + "") + case "p": + addParagraphIfNeeded() } return } @@ -91,7 +100,7 @@ func lineToAST(line string, state *GemLexerState, ast *[]Line) { goto numberState case "launchpad": goto launchpadState - default: + default: // "p" or "" goto normalState } @@ -175,43 +184,57 @@ normalState: switch { case startsWith("```"): + addParagraphIfNeeded() state.where = "pre" state.buf = fmt.Sprintf("
", state.id, strings.TrimPrefix(line, "```"))
 	case startsWith("* "):
+		addParagraphIfNeeded()
 		state.where = "list"
 		state.buf = fmt.Sprintf("
    \n", state.id) goto listState case startsWith("*. "): + addParagraphIfNeeded() state.where = "number" state.buf = fmt.Sprintf("
      \n", state.id) goto numberState case startsWith("###### "): + addParagraphIfNeeded() addHeading(6) case startsWith("##### "): + addParagraphIfNeeded() addHeading(5) case startsWith("#### "): + addParagraphIfNeeded() addHeading(4) case startsWith("### "): + addParagraphIfNeeded() addHeading(3) case startsWith("## "): + addParagraphIfNeeded() addHeading(2) case startsWith("# "): + addParagraphIfNeeded() addHeading(1) case startsWith(">"): + addParagraphIfNeeded() addLine(fmt.Sprintf( "
      %s
      ", state.id, remover(">")(line))) case startsWith("=>"): + addParagraphIfNeeded() state.where = "launchpad" state.buf = fmt.Sprintf("
        \n", state.id) goto launchpadState case startsWith("<="): + addParagraphIfNeeded() addLine(parseTransclusion(line, state.name)) case line == "----": + addParagraphIfNeeded() *ast = append(*ast, Line{id: -1, contents: "
        "}) case MatchesImg(line): + addParagraphIfNeeded() img, shouldGoBackToNormal := ImgFromFirstLine(line, state.name) if shouldGoBackToNormal { addLine(*img) @@ -220,9 +243,14 @@ normalState: state.img = img } case MatchesTable(line): + addParagraphIfNeeded() state.where = "table" state.table = TableFromFirstLine(line, state.name) + + case state.where == "p": + state.buf += "\n" + line default: - addLine(fmt.Sprintf("

        %s

        ", state.id, ParagraphToHtml(state.name, line))) + state.where = "p" + state.buf = line } } diff --git a/metarrhiza b/metarrhiza index 95f48bf..b67144c 160000 --- a/metarrhiza +++ b/metarrhiza @@ -1 +1 @@ -Subproject commit 95f48bfd7a7cfef17d56cef207a770767d727950 +Subproject commit b67144cc770900104a652483c11cabfecc7325aa