mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2024-12-12 05:20:26 +00:00
Gemini parser
This commit is contained in:
parent
05542ec17b
commit
0d9a677aca
@ -1,7 +1,73 @@
|
||||
package parser
|
||||
|
||||
import ()
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/util"
|
||||
)
|
||||
|
||||
const (
|
||||
linkToken = "=>"
|
||||
headerToken = "#"
|
||||
quoteToken = ">"
|
||||
preformattedToken = "```"
|
||||
listItemToken = "*"
|
||||
)
|
||||
|
||||
func GeminiToHtml(gemini []byte) string {
|
||||
return ""
|
||||
lines, _ := StringToLines(string(util.NormalizeEOL(gemini)))
|
||||
var html []string
|
||||
for _, line := range lines {
|
||||
html = append(html, geminiLineToHtml(line))
|
||||
}
|
||||
buffer := bytes.Buffer{}
|
||||
for _, line := range html {
|
||||
buffer.WriteString(line)
|
||||
}
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
func geminiLineToHtml(line string) (res string) {
|
||||
arr := strings.Fields(line)
|
||||
if len(arr) == 0 {
|
||||
return lineBreak
|
||||
}
|
||||
|
||||
content := arr[1:]
|
||||
token := arr[0]
|
||||
if string(token[0]) == headerToken {
|
||||
return makeHeader(makeOutHeader(arr))
|
||||
}
|
||||
|
||||
switch token {
|
||||
case linkToken:
|
||||
res = makeLink(makeOutLink(content))
|
||||
case quoteToken:
|
||||
res = makeBlockQuote(LinesToString(content, " "))
|
||||
case preformattedToken:
|
||||
res = makePreformatted(LinesToString(content, " "))
|
||||
case listItemToken:
|
||||
res = makeListItem(LinesToString(content, " "))
|
||||
default:
|
||||
res = makeParagraph(line)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func makeOutLink(arr []string) (source, content string) {
|
||||
switch len(arr) {
|
||||
case 0:
|
||||
return "", ""
|
||||
case 1:
|
||||
return arr[0], arr[0]
|
||||
default:
|
||||
return arr[0], LinesToString(arr[1:], " ")
|
||||
}
|
||||
}
|
||||
|
||||
func makeOutHeader(arr []string) (level int, content string) {
|
||||
level = len(arr[0])
|
||||
content = LinesToString(arr[1:], " ")
|
||||
return
|
||||
}
|
||||
|
53
plugin/parser/toHtml.go
Normal file
53
plugin/parser/toHtml.go
Normal file
@ -0,0 +1,53 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
lineBreak = "<br>"
|
||||
)
|
||||
|
||||
func makeLink(source, content string) string {
|
||||
return fmt.Sprintf(`<a href="%v">%v</a>`, source, content)
|
||||
}
|
||||
|
||||
func makeParagraph(content string) string {
|
||||
return `<p>` + content + `</p>`
|
||||
}
|
||||
|
||||
func makeBlockQuote(content string) string {
|
||||
return `<blockquote>` + content + `</blockquote>`
|
||||
}
|
||||
|
||||
func makeHeader(level int, content string) string {
|
||||
return fmt.Sprintf("<h%v>%v</h%v>", level, content, level)
|
||||
}
|
||||
|
||||
func makePreformatted(content string) string {
|
||||
return "<pre>" + content + "</pre>"
|
||||
}
|
||||
|
||||
func makeListItem(content string) string {
|
||||
return "<li>" + content + "</li>"
|
||||
}
|
||||
|
||||
func StringToLines(s string) (lines []string, err error) {
|
||||
scanner := bufio.NewScanner(strings.NewReader(s))
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
err = scanner.Err()
|
||||
return
|
||||
}
|
||||
|
||||
func LinesToString(lines []string, separator string) string {
|
||||
buffer := bytes.Buffer{}
|
||||
for _, line := range lines {
|
||||
buffer.WriteString(line + separator)
|
||||
}
|
||||
return buffer.String()
|
||||
}
|
25
wiki/main/testgemini/1.txt
Normal file
25
wiki/main/testgemini/1.txt
Normal file
@ -0,0 +1,25 @@
|
||||
# Gemtext cheatsheet
|
||||
Here's the basics of how text works in Gemtext:
|
||||
|
||||
Long lines get wrapped by the client to fit the screen
|
||||
Short lines *don't* get joined together
|
||||
Write paragraphs as single long lines
|
||||
Blank lines are rendered verbatim
|
||||
You get three levels of heading:
|
||||
|
||||
# Heading
|
||||
## Sub-heading
|
||||
### Sub-subheading
|
||||
You get one kind of list and you can't nest them:
|
||||
|
||||
* Mercury
|
||||
* Gemini
|
||||
* Apollo
|
||||
Here's a quote from Maciej Cegłowski:
|
||||
|
||||
> I contend that text-based websites should not exceed in size the major works of Russian literature.
|
||||
Lines which start with ``` will cause clients to toggle in and out of ordinary rendering mode and preformatted mode. In preformatted mode, Gemtext syntax is ignored so links etc. will not be rendered, and text will appear in a monospace font.
|
||||
``` =>https://not-a-href.com preformatted
|
||||
|
||||
=> https://proxy.vulpes.one/gemini/gemini.circumlunar.space/docs/cheatsheet.gmi Original cheatsheet
|
||||
=> https://proxy.vulpes.one/gemini/gemini.circumlunar.space/docs/cheatsheet.gmi
|
17
wiki/main/testgemini/meta.json
Normal file
17
wiki/main/testgemini/meta.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"views": 0,
|
||||
"deleted": false,
|
||||
"revisions": {
|
||||
"1": {
|
||||
"tags": null,
|
||||
"name": "Testgemini",
|
||||
"comment": "Create :Main/Testgemini",
|
||||
"author": "",
|
||||
"time": 1593960824,
|
||||
"text_mime": "text/gemini",
|
||||
"binary_mime": "",
|
||||
"text_name": "1.txt",
|
||||
"binary_name": ""
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user