1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-07-04 10:42:50 +00:00

Support formatting in headings

This commit is contained in:
bouncepaw 2020-11-12 23:21:49 +05:00
parent 8bc3fb9821
commit c111e95468
3 changed files with 10 additions and 32 deletions

View File

@ -59,6 +59,9 @@ func geminiLineToAST(line string, state *GemLexerState, ast *[]Line) {
startsWith := func(token string) bool { startsWith := func(token string) bool {
return strings.HasPrefix(line, token) return strings.HasPrefix(line, token)
} }
addHeading := func(i int) {
addLine(fmt.Sprintf("<h%d id='%d'>%s</h%d>", i, state.id, ParagraphToHtml(state.name, line[i+1:]), i))
}
// Beware! Usage of goto. Some may say it is considered evil but in this case it helped to make a better-structured code. // 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 { switch state.where {
@ -142,23 +145,17 @@ normalState:
goto numberState goto numberState
case startsWith("###### "): case startsWith("###### "):
addLine(fmt.Sprintf( addHeading(6)
"<h6 id='%d'>%s</h6>", state.id, line[7:]))
case startsWith("##### "): case startsWith("##### "):
addLine(fmt.Sprintf( addHeading(5)
"<h5 id='%d'>%s</h5>", state.id, line[6:]))
case startsWith("#### "): case startsWith("#### "):
addLine(fmt.Sprintf( addHeading(4)
"<h4 id='%d'>%s</h4>", state.id, line[5:]))
case startsWith("### "): case startsWith("### "):
addLine(fmt.Sprintf( addHeading(3)
"<h3 id='%d'>%s</h3>", state.id, line[4:]))
case startsWith("## "): case startsWith("## "):
addLine(fmt.Sprintf( addHeading(2)
"<h2 id='%d'>%s</h2>", state.id, line[3:]))
case startsWith("# "): case startsWith("# "):
addLine(fmt.Sprintf( addHeading(1)
"<h1 id='%d'>%s</h1>", state.id, line[2:]))
case startsWith(">"): case startsWith(">"):
addLine(fmt.Sprintf( addLine(fmt.Sprintf(

@ -1 +1 @@
Subproject commit d78a90718ae9c36f7a114901ddad84b0e23221b3 Subproject commit 8d595c930664f271e58d3cfb3fa12c6feabfec3c

View File

@ -1,19 +0,0 @@
package main
import (
"testing"
)
func TestMimeData(t *testing.T) {
check := func(ext string, expectedIsText bool, expectedMimeId int) {
isText, mimeId := mimeData(ext)
if isText != expectedIsText || mimeId != expectedMimeId {
t.Error(ext, isText, mimeId)
}
}
check(".txt", true, int(TextPlain))
check(".gmi", true, int(TextGemini))
check(".bin", false, int(BinaryOctet))
check(".jpg", false, int(BinaryJpeg))
check(".bin", false, int(BinaryOctet))
}