diff --git a/cmd/web/main.go b/cmd/web/main.go index 2fa4377..1163b5d 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "context" "crypto/md5" "crypto/tls" @@ -23,6 +24,7 @@ import ( "github.com/go-enry/go-enry/v2" "github.com/google/uuid" "github.com/microcosm-cc/bluemonday" + "github.com/niklasfasching/go-org/org" "github.com/russross/blackfriday" _ "modernc.org/sqlite" "tailscale.com/client/tailscale" @@ -509,15 +511,37 @@ WHERE p.id = ?1` cssClass = fmt.Sprintf("lang-%s", strings.ToLower(lang)) } + p := bluemonday.UGCPolicy() + p.AllowAttrs("class").Matching(regexp.MustCompile("^language-[a-zA-Z0-9]+$")).OnElements("code") + if lang == "Markdown" { output := blackfriday.MarkdownCommon([]byte(data)) - p := bluemonday.UGCPolicy() - p.AllowAttrs("class").Matching(regexp.MustCompile("^language-[a-zA-Z0-9]+$")).OnElements("code") sanitized := p.SanitizeBytes(output) raw := template.HTML(string(sanitized)) rawHTML = &raw } + if lang == "Org" { + w := org.NewHTMLWriter() + w.HighlightCodeBlock = func(source, lang string, inline bool, params map[string]string) string { + sourceSanitized := p.SanitizeBytes([]byte(source)) + if inline { + return fmt.Sprintf("%s", sourceSanitized) + + } + return fmt.Sprintf("
%s
", lang, sourceSanitized) + } + output, err := org.New().Parse(bytes.NewReader([]byte(data)), "").Write(w) + // If we fail parsing just fall back to text and log. + if err == nil { + sanitized := p.SanitizeBytes([]byte(output)) + raw := template.HTML(string(sanitized)) + rawHTML = &raw + } else { + log.Printf("error parsing org file: %s", err) + } + } + // If you specify a formatting option: if len(pathComponents) != 1 { switch pathComponents[1] { @@ -540,15 +564,24 @@ WHERE p.id = ?1` return case "": // view markdown file with a fancy HTML rendering step - case "md": - if lang != "Markdown" { + case "md", "org": + if lang != "Markdown" && lang != "Org" { http.Redirect(w, r, "/paste/"+id, http.StatusTemporaryRedirect) return } - title, ok := strings.CutPrefix(strings.Split(strings.TrimSpace(data), "\n")[0], "#") - if !ok { - title = fname + title := fname + if lang == "Markdown" { + mdTitle, ok := strings.CutPrefix(strings.Split(strings.TrimSpace(data), "\n")[0], "#") + if ok { + title = mdTitle + } + } + if lang == "Org" { + ogTitle, ok := strings.CutPrefix(strings.Split(strings.TrimSpace(data), "\n")[0], "#+title:") + if ok { + title = ogTitle + } } err = s.tmpls.ExecuteTemplate(w, "fancypost.html", struct { diff --git a/cmd/web/tmpl/fancypost.html b/cmd/web/tmpl/fancypost.html index ab923dc..917d5a0 100644 --- a/cmd/web/tmpl/fancypost.html +++ b/cmd/web/tmpl/fancypost.html @@ -24,7 +24,11 @@ padding: 20px; font-size: 0.875em; } - } + } + + img { + max-width: 100%; + } diff --git a/go.mod b/go.mod index 578289a..a184b75 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/go-enry/go-enry/v2 v2.8.4 github.com/google/uuid v1.3.1 github.com/microcosm-cc/bluemonday v1.0.25 + github.com/niklasfasching/go-org v1.7.0 github.com/russross/blackfriday v1.6.0 modernc.org/sqlite v1.25.0 tailscale.com v1.48.2 diff --git a/go.sum b/go.sum index 272f133..b3527fb 100644 --- a/go.sum +++ b/go.sum @@ -165,6 +165,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/niklasfasching/go-org v1.7.0 h1:vyMdcMWWTe/XmANk19F4k8XGBYg0GQ/gJGMimOjGMek= +github.com/niklasfasching/go-org v1.7.0/go.mod h1:WuVm4d45oePiE0eX25GqTDQIt/qPW1T9DGkRscqLW5o= github.com/pierrec/lz4/v4 v4.1.14/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc= github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= diff --git a/gomod2nix.toml b/gomod2nix.toml index 5c3d795..4212196 100644 --- a/gomod2nix.toml +++ b/gomod2nix.toml @@ -58,12 +58,12 @@ schema = 3 [mod."github.com/coreos/go-iptables"] version = "v0.6.0" hash = "sha256-hB+9XsybJuEFozAXo+G8+2qMMZGoqBbMmSwmcNJgmSU=" - [mod."github.com/dustin/go-humanize"] - version = "v1.0.1" - hash = "sha256-yuvxYYngpfVkUg9yAmG99IUVmADTQA0tMbBXe0Fq0Mc=" [mod."github.com/dblohm7/wingoes"] version = "v0.0.0-20230803162905-5c6286bb8c6e" hash = "sha256-YPZP7Hq96I9+3gHyLp0sTsk0IN2wWSJXGHh+kJNRgAk=" + [mod."github.com/dustin/go-humanize"] + version = "v1.0.1" + hash = "sha256-yuvxYYngpfVkUg9yAmG99IUVmADTQA0tMbBXe0Fq0Mc=" [mod."github.com/fxamacker/cbor/v2"] version = "v2.4.0" hash = "sha256-LwOrfDN5fx/R2lgaD90RIu2NxcJDpyqYEURLc9Kgxgw=" @@ -113,8 +113,8 @@ schema = 3 version = "v1.1.1-0.20230202152459-5c7d0dd6ab86" hash = "sha256-dgyrLXuM55z8FAoUjyt5TDlzim6HfphWo5wx1/DHLwE=" [mod."github.com/jsimonetti/rtnetlink"] - version = "v1.1.2-0.20220408201609-d380b505068b" - hash = "sha256-xHfsLwbycxNNyFI9zTfSMsb2T35BKEWyW6Fmk1tDWM4=" + version = "v1.3.2" + hash = "sha256-clvnQA5M4NHgv03tGDmdFmcfSjoqeZC3Gfy30rtFECI=" [mod."github.com/kballard/go-shellquote"] version = "v0.0.0-20180428030007-95032a82bc51" hash = "sha256-AOEdKETBMUC39ln6jBJ9NYdJWp++jV5lSbjNqG3dV+c=" @@ -125,8 +125,8 @@ schema = 3 version = "v0.0.0-20200729010619-da482cc4850a" hash = "sha256-lnr9r/KNv4EeeNohFImC3Vd5E9nJ0N+4ZZ0VHFjwHps=" [mod."github.com/mattn/go-isatty"] - version = "v0.0.16" - hash = "sha256-YMaPZvShDfA98vqw1+zWWl7M1IT4nHPGBrAt7kHo8Iw=" + version = "v0.0.18" + hash = "sha256-QpIn0DSggtBn2ocyj0RlXDKLK5F5KZG1/ogzrqBCjF8=" [mod."github.com/mdlayher/genetlink"] version = "v1.3.2" hash = "sha256-pgwXkyDY1dlB8tmV1lQ0Bz/2g0zmJOyXvQjacACy924=" @@ -148,6 +148,9 @@ schema = 3 [mod."github.com/mitchellh/go-ps"] version = "v1.0.0" hash = "sha256-HzxVHNLHZpnsBuPcub0G+9jjDcDOsxM/6wifbsxf7EY=" + [mod."github.com/niklasfasching/go-org"] + version = "v1.7.0" + hash = "sha256-i3NdcfER5JSIJv3GIJxeNJJpOyxiyxQKPZPpK2teQt4=" [mod."github.com/pierrec/lz4/v4"] version = "v4.1.17" hash = "sha256-36L+GNhRrHBCyhbHCqweCk5rfmggdRtHqH6g5m1ViQI="