From 3c6e2aa16f931f6e84e5fdbc4f031cfc11b90ab8 Mon Sep 17 00:00:00 2001 From: bouncepaw Date: Wed, 4 Nov 2020 22:42:02 +0500 Subject: [PATCH] Add inline links --- markup/img.go | 2 +- markup/img_test.go | 16 ++++++------ markup/lexer.go | 55 ++++----------------------------------- markup/lexer_test.go | 14 +++++----- markup/link.go | 49 ++++++++++++++++++++++++++++++++++ markup/paragraph.go | 38 ++++++++++++++++++++++++--- markup/paragraph_test.go | 9 +++++-- markup/testdata/test.myco | 8 +++--- metarrhiza | 2 +- 9 files changed, 117 insertions(+), 76 deletions(-) create mode 100644 markup/link.go diff --git a/markup/img.go b/markup/img.go index 18e75d8b..244451b8 100644 --- a/markup/img.go +++ b/markup/img.go @@ -127,7 +127,7 @@ func (img Img) ToHtml() (html string) { if i > 0 { html += `
` } - html += ParagraphToHtml(line) + html += ParagraphToHtml(img.hyphaName, line) } } html += `` diff --git a/markup/img_test.go b/markup/img_test.go index ea9b6f29..f2e20448 100644 --- a/markup/img_test.go +++ b/markup/img_test.go @@ -12,14 +12,14 @@ func TestParseStartOfEntry(t *testing.T) { entry imgEntry followedByDesc bool }{ - {"apple", imgEntry{"apple", "", "", ""}, false}, - {"pear:", imgEntry{"pear", "", "", ""}, false}, - {"яблоко: 30*60", imgEntry{"яблоко", "30", "60", ""}, false}, - {"груша : 65 ", imgEntry{"груша", "65", "", ""}, false}, - {"жеронимо : 30 { full desc }", imgEntry{"жеронимо", "30", "", " full desc "}, false}, - {"жорно жованна : *5555 {partial description", imgEntry{"жорно_жованна", "", "5555", "partial description"}, true}, - {"иноске : {full}", imgEntry{"иноске", "", "", "full"}, false}, - {"j:{partial", imgEntry{"j", "", "", "partial"}, true}, + {"apple", imgEntry{"/binary/apple", "", "", ""}, false}, + {"pear|", imgEntry{"/binary/pear", "", "", ""}, false}, + {"яблоко| 30*60", imgEntry{"/binary/яблоко", "30", "60", ""}, false}, + {"груша | 65 ", imgEntry{"/binary/груша", "65", "", ""}, false}, + {"жеронимо | 30 { full desc }", imgEntry{"/binary/жеронимо", "30", "", " full desc "}, false}, + {"жорно жованна | *5555 {partial description", imgEntry{"/binary/жорно_жованна", "", "5555", "partial description"}, true}, + {"иноске | {full}", imgEntry{"/binary/иноске", "", "", "full"}, false}, + {"j|{partial", imgEntry{"/binary/j", "", "", "partial"}, true}, } for _, triplet := range tests { entry, followedByDesc := img.parseStartOfEntry(triplet.line) diff --git a/markup/lexer.go b/markup/lexer.go index b29b7fcd..cad18d72 100644 --- a/markup/lexer.go +++ b/markup/lexer.go @@ -3,7 +3,6 @@ package markup import ( "fmt" "html" - "path" "strings" ) @@ -31,50 +30,6 @@ type Line struct { contents interface{} } -// Parse markup line starting with "=>" according to wikilink rules. -// See http://localhost:1737/page/wikilink -func wikilink(src string, state *GemLexerState) (href, text, class string) { - src = strings.TrimSpace(remover("=>")(src)) - if src == "" { - return - } - // Href is text after => till first whitespace - href = strings.Fields(src)[0] - // Text is everything after whitespace. - // If there's no text, make it same as href - if text = strings.TrimPrefix(src, href); text == "" { - text = href - } - - class = "wikilink_internal" - - switch { - case strings.HasPrefix(href, "./"): - hyphaName := canonicalName(path.Join( - state.name, strings.TrimPrefix(href, "./"))) - if !HyphaExists(hyphaName) { - class += " wikilink_new" - } - href = path.Join("/page", hyphaName) - case strings.HasPrefix(href, "../"): - hyphaName := canonicalName(path.Join( - path.Dir(state.name), strings.TrimPrefix(href, "../"))) - if !HyphaExists(hyphaName) { - class += " wikilink_new" - } - href = path.Join("/page", hyphaName) - case strings.HasPrefix(href, "/"): - case strings.ContainsRune(href, ':'): - class = "wikilink_external" - default: - if !HyphaExists(canonicalName(href)) { - class += " wikilink_new" - } - href = path.Join("/page", href) - } - return href, strings.TrimSpace(text), class -} - func lex(name, content string) (ast []Line) { var state = GemLexerState{name: name} @@ -141,7 +96,7 @@ preformattedState: listState: switch { case startsWith("* "): - state.buf += fmt.Sprintf("\t
  • %s
  • \n", ParagraphToHtml(line[2:])) + state.buf += fmt.Sprintf("\t
  • %s
  • \n", ParagraphToHtml(state.name, line[2:])) case startsWith("```"): state.where = "pre" addLine(state.buf + "") @@ -157,7 +112,7 @@ listState: numberState: switch { case startsWith("*. "): - state.buf += fmt.Sprintf("\t
  • %s
  • \n", ParagraphToHtml(line[3:])) + state.buf += fmt.Sprintf("\t
  • %s
  • \n", ParagraphToHtml(state.name, line[3:])) case startsWith("```"): state.where = "pre" addLine(state.buf + "") @@ -209,9 +164,9 @@ normalState: addLine(fmt.Sprintf( "
    %s
    ", state.id, remover(">")(line))) case startsWith("=>"): - source, content, class := wikilink(line, state) + href, text, class := Rocketlink(line, state.name) addLine(fmt.Sprintf( - `

    %s

    `, state.id, class, source, content)) + `

    %s

    `, state.id, class, href, text)) case startsWith("<="): addLine(parseTransclusion(line, state.name)) @@ -221,6 +176,6 @@ normalState: state.where = "img" state.img = ImgFromFirstLine(line, state.name) default: - addLine(fmt.Sprintf("

    %s

    ", state.id, ParagraphToHtml(line))) + addLine(fmt.Sprintf("

    %s

    ", state.id, ParagraphToHtml(state.name, line))) } } diff --git a/markup/lexer_test.go b/markup/lexer_test.go index 41dc64b4..3298a360 100644 --- a/markup/lexer_test.go +++ b/markup/lexer_test.go @@ -41,13 +41,13 @@ func TestLex(t *testing.T) { `}, {6, "

    text

    "}, {7, "

    more text

    "}, - {8, `

    some link

    `}, + {8, `

    some link

    `}, {9, ``}, {10, `
    => preformatted text
     where markup is not lexed
    `}, - {11, `

    linking

    `}, + {11, `

    linking

    `}, {12, "

    text

    "}, {13, `
    ()
     /\
    `}, @@ -56,11 +56,11 @@ where markup is not lexed`}, hyphaName: "Apple", inDesc: false, entries: []imgEntry{ - {"hypha1", "", "", ""}, - {"hypha2", "", "", ""}, - {"hypha3", "60", "", ""}, - {"hypha4", "", "", " line1\nline2\n"}, - {"hypha5", "", "", "\nstate of minnesota\n"}, + {"/binary/hypha1", "", "", ""}, + {"/binary/hypha2", "", "", ""}, + {"/binary/hypha3", "60", "", ""}, + {"/binary/hypha4", "", "", " line1\nline2\n"}, + {"/binary/hypha5", "", "", "\nstate of minnesota\n"}, }, }}, }) diff --git a/markup/link.go b/markup/link.go new file mode 100644 index 00000000..eb52c636 --- /dev/null +++ b/markup/link.go @@ -0,0 +1,49 @@ +package markup + +import ( + "path" + "strings" +) + +// LinkParts determines what href, text and class should resulting have based on mycomarkup's addr, display and hypha name. +// +// => addr display +// [[addr|display]] +func LinkParts(addr, display, hyphaName string) (href, text, class string) { + if display == "" { + text = addr + } else { + text = strings.TrimSpace(display) + } + class = "wikilink_internal" + + switch { + case strings.ContainsRune(addr, ':'): + return addr, text, "wikilink_external" + case strings.HasPrefix(addr, "/"): + return addr, text, class + case strings.HasPrefix(addr, "./"): + hyphaName = canonicalName(path.Join(hyphaName, addr[2:])) + case strings.HasPrefix(addr, "../"): + hyphaName = canonicalName(path.Join(path.Dir(hyphaName), addr[3:])) + default: + hyphaName = canonicalName(addr) + } + if !HyphaExists(hyphaName) { + class += " wikilink_new" + } + return "/page/" + hyphaName, text, class +} + +// Parse markup line starting with "=>" according to wikilink rules. +// See http://localhost:1737/page/wikilink +func Rocketlink(src, hyphaName string) (href, text, class string) { + src = strings.TrimSpace(src[2:]) // Drop => + if src == "" { + return + } + // Href is text after => till first whitespace + addr := strings.Fields(src)[0] + display := strings.TrimPrefix(src, addr) + return LinkParts(addr, display, hyphaName) +} diff --git a/markup/paragraph.go b/markup/paragraph.go index 75070a4c..1eb1f9bb 100644 --- a/markup/paragraph.go +++ b/markup/paragraph.go @@ -17,6 +17,7 @@ const ( spanSuper spanSub spanMark + spanLink ) func tagFromState(stt spanTokenType, tagState map[spanTokenType]bool, tagName, originalForm string) string { @@ -32,7 +33,33 @@ func tagFromState(stt spanTokenType, tagState map[spanTokenType]bool, tagName, o } } -// getTextNode splits the `p` into two parts `textNode` and `rest` by the first encountered rune that resembles a span tag. If there is none, `textNode = p`, `rest = ""`. It handles escaping with backslash. +func getLinkNode(input *bytes.Buffer, hyphaName string) string { + input.Next(2) + var ( + escaping = false + addrBuf = bytes.Buffer{} + displayBuf = bytes.Buffer{} + currBuf = &addrBuf + ) + for input.Len() != 0 { + b, _ := input.ReadByte() + if escaping { + currBuf.WriteByte(b) + escaping = false + } else if b == '|' && currBuf == &addrBuf { + currBuf = &displayBuf + } else if b == ']' && bytes.HasPrefix(input.Bytes(), []byte{']'}) { + input.Next(1) + break + } else { + currBuf.WriteByte(b) + } + } + href, text, class := LinkParts(addrBuf.String(), displayBuf.String(), hyphaName) + return fmt.Sprintf(`%s`, href, class, html.EscapeString(text)) +} + +// getTextNode splits the `input` into two parts `textNode` and `rest` by the first encountered rune that resembles a span tag. If there is none, `textNode = input`, `rest = ""`. It handles escaping with backslash. func getTextNode(input *bytes.Buffer) string { var ( textNodeBuffer = bytes.Buffer{} @@ -51,7 +78,7 @@ func getTextNode(input *bytes.Buffer) string { escaping = false } else if b == '\\' { escaping = true - } else if strings.IndexByte("/*`^,!", b) >= 0 { + } else if strings.IndexByte("/*`^,![", b) >= 0 { input.UnreadByte() break } else { @@ -61,7 +88,7 @@ func getTextNode(input *bytes.Buffer) string { return textNodeBuffer.String() } -func ParagraphToHtml(input string) string { +func ParagraphToHtml(hyphaName, input string) string { var ( p = bytes.NewBufferString(input) ret strings.Builder @@ -73,6 +100,7 @@ func ParagraphToHtml(input string) string { spanSuper: false, spanSub: false, spanMark: false, + spanLink: false, } startsWith = func(t string) bool { return bytes.HasPrefix(p.Bytes(), []byte(t)) @@ -99,6 +127,8 @@ func ParagraphToHtml(input string) string { case startsWith("!!"): ret.WriteString(tagFromState(spanMark, tagState, "mark", "!!")) p.Next(2) + case startsWith("[["): + ret.WriteString(getLinkNode(p, hyphaName)) default: ret.WriteString(html.EscapeString(getTextNode(p))) } @@ -119,6 +149,8 @@ func ParagraphToHtml(input string) string { ret.WriteString(tagFromState(spanSub, tagState, "sub", ",,")) case spanMark: ret.WriteString(tagFromState(spanMark, tagState, "mark", "!!")) + case spanLink: + ret.WriteString(tagFromState(spanLink, tagState, "a", "[[")) } } } diff --git a/markup/paragraph_test.go b/markup/paragraph_test.go index 49505a99..6fd1d918 100644 --- a/markup/paragraph_test.go +++ b/markup/paragraph_test.go @@ -35,10 +35,15 @@ func TestParagraphToHtml(t *testing.T) { {"this is a left **bold", "this is a left bold"}, {"this line has a ,comma, two of them", "this line has a ,comma, two of them"}, {"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."}, + {"A [[simple]] link", `A simple link`}, } for _, test := range tests { - if ParagraphToHtml(test[0]) != test[1] { - t.Error(fmt.Sprintf("%q: Wanted %q, got %q", test[0], test[1], ParagraphToHtml(test[0]))) + if ParagraphToHtml("Apple", test[0]) != test[1] { + t.Error(fmt.Sprintf("%q: Wanted %q, got %q", test[0], test[1], ParagraphToHtml("Apple", test[0]))) } } } + +func init() { + HyphaExists = func(_ string) bool { return true } +} diff --git a/markup/testdata/test.myco b/markup/testdata/test.myco index 389188e3..fd5880fc 100644 --- a/markup/testdata/test.myco +++ b/markup/testdata/test.myco @@ -25,13 +25,13 @@ text img { hypha1 -hypha2: -hypha3: 60 -hypha4: { line1 +hypha2| +hypha3| 60 +hypha4| { line1 line2 } this is ignored -hypha5: { +hypha5| { state of minnesota } } diff --git a/metarrhiza b/metarrhiza index 9fa5334e..d78a9071 160000 --- a/metarrhiza +++ b/metarrhiza @@ -1 +1 @@ -Subproject commit 9fa5334ee958c02fbb327c002bf646e648a5a1be +Subproject commit d78a90718ae9c36f7a114901ddad84b0e23221b3