From 8dfd2fcf9bdfe9957106d4ded4cec39b864ce8af Mon Sep 17 00:00:00 2001 From: hugmouse Date: Fri, 5 Mar 2021 14:34:56 +0800 Subject: [PATCH 1/2] Fix lexer for "----" (
) case The point is that there may be another character in the string. For example, a space or a line break. Because we check the whole string, not the first 4 characters - we can ignore the variant where it can be "----\s" or "----\r\n" and make the wrong markup. Alternatively: use `startsWith("----")`, but this is a bit more expensive operation. --- markup/lexer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/markup/lexer.go b/markup/lexer.go index 607393f..fbd87fc 100644 --- a/markup/lexer.go +++ b/markup/lexer.go @@ -235,7 +235,7 @@ normalState: case startsWith("<="): addParagraphIfNeeded() addLine(parseTransclusion(line, state.name)) - case line == "----": + case line[:4] == "----": addParagraphIfNeeded() *ast = append(*ast, Line{id: -1, contents: "
"}) case MatchesImg(line): From 62c616985d8bb70898fc13093d2bccb454ea82bd Mon Sep 17 00:00:00 2001 From: hugmouse Date: Fri, 5 Mar 2021 16:29:39 +0800 Subject: [PATCH 2/2] Replace old method with new that is a little overcomplicated --- markup/hr.go | 34 ++++++++++++++++++++++++++++++++++ markup/lexer.go | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 markup/hr.go diff --git a/markup/hr.go b/markup/hr.go new file mode 100644 index 0000000..278138a --- /dev/null +++ b/markup/hr.go @@ -0,0 +1,34 @@ +package markup + +import ( + "unicode" +) + +// MatchesHorizontalLine checks if the string can be interpreted as suitable for rendering as
. +// +// The rule is: if there are more than 4 characters "-" in the string, then make it a horizontal line. +// Otherwise it is a paragraph (

). +func MatchesHorizontalLine(line string) bool { + counter := 0 + + // Check initially that the symbol is "-". If it is not a "-", it is most likely a space or another character. + // With unicode.IsLetter() we can separate spaces and characters. + for _, ch := range line { + if ch == '-' { + counter++ + continue + } + // If we bump into any other character (letter) in the line, it is immediately an incorrect horizontal line. + // There is no point in counting further, we end the loop. + if unicode.IsLetter(ch) { + counter = 0 + break + } + } + + if counter >= 4 { + return true + } + + return false +} diff --git a/markup/lexer.go b/markup/lexer.go index fbd87fc..bf077e7 100644 --- a/markup/lexer.go +++ b/markup/lexer.go @@ -235,7 +235,7 @@ normalState: case startsWith("<="): addParagraphIfNeeded() addLine(parseTransclusion(line, state.name)) - case line[:4] == "----": + case MatchesHorizontalLine(line): addParagraphIfNeeded() *ast = append(*ast, Line{id: -1, contents: "


"}) case MatchesImg(line):