From c56128156e7a81868b800c27569d84e7a9ff0cd7 Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Tue, 7 Feb 2023 01:13:17 +0000 Subject: [PATCH] gotta commit --- cmd/main.go | 15 ++++++++ go.mod | 3 ++ index.md | 10 +++++ lexer/lexer.go | 92 +++++++++++++++++++++++++++++++++++++++++++++ lexer/lexer_test.go | 29 ++++++++++++++ sanitize.sh | 4 ++ test-cases | 10 +++++ 7 files changed, 163 insertions(+) create mode 100644 cmd/main.go create mode 100644 go.mod create mode 100644 index.md create mode 100644 lexer/lexer.go create mode 100644 lexer/lexer_test.go create mode 100755 sanitize.sh create mode 100644 test-cases diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..77cf9e0 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "log" + + "github.com/therealfakemoot/obsidian-parser" +) + +func main() { + l, tokens := lexer.Lex("[[foo/bar/butts.png]]") + go l.Run() + for t := range tokens { + log.Printf("%#v\n", t) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a1da3a2 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/therealfakemoot/obsidian-parser + +go 1.19 diff --git a/index.md b/index.md new file mode 100644 index 0000000..1b7d690 --- /dev/null +++ b/index.md @@ -0,0 +1,10 @@ +At this point, I've got a fully automated publishing pipeline. As soon as a commit gets pushed to my blog repository, Drone jumps into action and runs a fresh Hugo build. The process is far from perfect, though. + +![[Resources/attachments/obsidian-pipeline-screenshot.png]] +[[Resources/attachments/obsidian-pipeline-screenshot.png]] + +For some reason, `audit2allow` was emitting invalid output as the result of something in my audit log. I never traced it down. Whatever was causing this wasn't related to my `drone` setup since I got everything running without fixing it. + +[[Resources/notes/202203261920-faerun-atropus|black moon]] ![[Resources/attachments/obsidian-pipeline-screenshot.png]] +[[Resources/notes/202203261920-faerun-atropus|black moon]]![[Resources/attachments/obsidian-pipeline-screenshot.png]] +![[Resources/attachments/obsidian-pipeline-screenshot.png]][[Resources/attachments/obsidian-pipeline-screenshot.png]] diff --git a/lexer/lexer.go b/lexer/lexer.go new file mode 100644 index 0000000..0a44c7a --- /dev/null +++ b/lexer/lexer.go @@ -0,0 +1,92 @@ +package lexer + +import ( + "bufio" + // "log" + "strings" +) + +type Token struct { + Raw string +} + +var ( + LinkBeginToken = Token{Raw: "[["} + LinkEndToken = Token{Raw: "]]"} + LinkTextToken = Token{} + LinkAliasBeginToken = Token{Raw: "|"} + LinkAliasTextToken = Token{} + LinkSectionBeginToken = Token{Raw: "#"} + LinkSectionTextToken = Token{} + LinkBlockBeginToken = Token{Raw: "^"} + LinkBlockTextToken = Token{} +) + +type Lexer struct { + input []string + pos int + Tokens chan Token +} + +func Lex(input string) (*Lexer, chan Token) { + l := &Lexer{ + input: make([]string, 0), + pos: 0, + Tokens: make(chan Token), + } + + scanner := bufio.NewScanner(strings.NewReader(input)) + scanner.Split(bufio.ScanRunes) + + for scanner.Scan() { + l.input = append(l.input, scanner.Text()) + } + + go l.Run() + return l, l.Tokens +} + +func (l *Lexer) Next() string { + if l.pos > len(l.input)-1 { + return "eof" + } + r := l.input[l.pos] + l.pos++ + return r +} + +func (l *Lexer) Backup() { + l.pos-- +} + +func (l *Lexer) Peek() string { + r := l.Next() + l.Backup() + return r +} + +func (l *Lexer) Emit(t Token) { + // log.Printf("token emitted: %#v\n", t) + l.Tokens <- t +} + +func (l *Lexer) Run() { + defer close(l.Tokens) + for { + s := l.Next() + if s == "eof" { + return + } + + if s == "[" && l.Peek() == "[" { + l.Next() + l.Emit(LinkBeginToken) + } + + if s == "]" && l.Peek() == "]" { + l.Next() + l.Emit(LinkBeginToken) + } + + } +} diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go new file mode 100644 index 0000000..eb6ebfb --- /dev/null +++ b/lexer/lexer_test.go @@ -0,0 +1,29 @@ +package lexer + +import ( + "testing" +) + +func Test_Lexer(t *testing.T) { + tt := []string{ + "[[Regular Link]]", + /* + "![[Transcluded Link]]", + "[[Regular Link|Alias]]", + "[[Regular Link#Subsection of page]]", + "[[Regular Link^link to block]]", + "[[Regular Link#Subsection of page|Alias]]", + "[[Regular Link^link to block|Alias]]", + "[[Regular Link\|Alias]]", + "[[Regular Link^link to block\|Alias]]", + "[[Regular Link#Subsection of page\|Alias]]", + */ + } + + for _, tc := range tt { + t.Run(tc, func(t *testing.T) { + t.Fail() + }) + } + +} diff --git a/sanitize.sh b/sanitize.sh new file mode 100755 index 0000000..5056492 --- /dev/null +++ b/sanitize.sh @@ -0,0 +1,4 @@ +#! /bin/sh + +sed "s/!?\]\]/\)/g" $1 +sed "s/!?\[\[/\[\]\(/g" $1 diff --git a/test-cases b/test-cases new file mode 100644 index 0000000..cb4df09 --- /dev/null +++ b/test-cases @@ -0,0 +1,10 @@ +[[Regular Link]] +![[Transcluded Link]] +[[Regular Link|Alias]] +[[Regular Link#Subsection of page]] +[[Regular Link^link to block]] +[[Regular Link#Subsection of page|Alias]] +[[Regular Link^link to block|Alias]] +[[Regular Link\|Alias]] +[[Regular Link^link to block\|Alias]] +[[Regular Link#Subsection of page\|Alias]]