From 34d16f28f2d85bc1762e65a73d426de4ac8c30ed Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Tue, 30 Sep 2025 16:35:46 +0200 Subject: feat(markdown): simple lexer --- README.md | 5 +++- design/index.html | 2 +- design/log.html | 2 +- go.mod | 3 +++ mardown/lexer.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 go.mod create mode 100644 mardown/lexer.go diff --git a/README.md b/README.md index 32fe710..1eddd2b 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,6 @@ It aims to be simple, minimalist, brutalist, indie, and personnal. ## How it works Backend written in modern Go. -[gomarkdown/markdown](https://github.com/gomarkdown/markdown) looks like to be a good library. Light CSS, light JS, runs everywhere. SSR first. @@ -29,3 +28,7 @@ This repository contains only the source code of the website, not the contents. Mock-ups are in `design`. Only HTML and raw CSS here. +This project uses a custom markdown parser. +It is located in `markdown`. +I made my custom parser because I have extended its specification. + diff --git a/design/index.html b/design/index.html index 64a8ed5..da598ec 100644 --- a/design/index.html +++ b/design/index.html @@ -58,7 +58,7 @@ diff --git a/design/log.html b/design/log.html index fd38f9a..709e918 100644 --- a/design/log.html +++ b/design/log.html @@ -88,7 +88,7 @@ func main() { diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..37b3f25 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.anhgelus.world/anhgelus/small-world + +go 1.25.1 diff --git a/mardown/lexer.go b/mardown/lexer.go new file mode 100644 index 0000000..a7b364d --- /dev/null +++ b/mardown/lexer.go @@ -0,0 +1,72 @@ +package mardown + +import "fmt" + +type lexerType string + +const ( + lexerBreak lexerType = "break" + + lexerModifier lexerType = "modifier" + lexerCode lexerType = "code" + lexerHeader lexerType = "header" + lexerQuote lexerType = "quote" + + lexerLiteral lexerType = "literal" +) + +type lexer struct { + Type lexerType + Value string +} + +func (l *lexer) String() string { + return fmt.Sprintf("%s(%s)", l.Type, l.Value) +} + +type lexers struct { + current int + lexers []lexer +} + +func (l *lexers) Next() lexer { + l.current++ + return l.lexers[l.current] +} + +func (l *lexers) Finished() bool { + return l.current+1 >= len(l.lexers) +} + +func lex(s string) *lexers { + lxs := &lexers{current: -1} + var lexs []lexer + var currentType lexerType + var previous string + fn := func(c rune, t lexerType) { + if currentType != t && len(previous) > 0 { + lexs = append(lexs, lexer{Type: currentType, Value: previous}) + previous = "" + } + currentType = t + previous += string(c) + } + for _, c := range []rune(s) { + switch c { + case '*', '_': + fn(c, lexerModifier) + case '`': + fn(c, lexerCode) + case '\n': + fn(c, lexerBreak) + case '#': + fn(c, lexerHeader) + case '>': + fn(c, lexerQuote) + default: + fn(c, lexerLiteral) + } + } + lxs.lexers = lexs + return lxs +} -- cgit v1.2.3