Merge pull request #18 from gmemstr/org-mode-rendering

Treat org-mode as render-able document
This commit is contained in:
Xe Iaso 2023-09-19 11:59:42 -04:00 committed by GitHub
commit 4085af8a6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 15 deletions

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"bytes"
"context" "context"
"crypto/md5" "crypto/md5"
"crypto/tls" "crypto/tls"
@ -23,6 +24,7 @@ import (
"github.com/go-enry/go-enry/v2" "github.com/go-enry/go-enry/v2"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/microcosm-cc/bluemonday" "github.com/microcosm-cc/bluemonday"
"github.com/niklasfasching/go-org/org"
"github.com/russross/blackfriday" "github.com/russross/blackfriday"
_ "modernc.org/sqlite" _ "modernc.org/sqlite"
"tailscale.com/client/tailscale" "tailscale.com/client/tailscale"
@ -509,15 +511,37 @@ WHERE p.id = ?1`
cssClass = fmt.Sprintf("lang-%s", strings.ToLower(lang)) cssClass = fmt.Sprintf("lang-%s", strings.ToLower(lang))
} }
if lang == "Markdown" {
output := blackfriday.MarkdownCommon([]byte(data))
p := bluemonday.UGCPolicy() p := bluemonday.UGCPolicy()
p.AllowAttrs("class").Matching(regexp.MustCompile("^language-[a-zA-Z0-9]+$")).OnElements("code") p.AllowAttrs("class").Matching(regexp.MustCompile("^language-[a-zA-Z0-9]+$")).OnElements("code")
if lang == "Markdown" {
output := blackfriday.MarkdownCommon([]byte(data))
sanitized := p.SanitizeBytes(output) sanitized := p.SanitizeBytes(output)
raw := template.HTML(string(sanitized)) raw := template.HTML(string(sanitized))
rawHTML = &raw 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("<code>%s</code>", sourceSanitized)
}
return fmt.Sprintf("<pre class=\"language-%[1]s\"><code class=\"language-%[1]s\">%s</code></pre>", 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 you specify a formatting option:
if len(pathComponents) != 1 { if len(pathComponents) != 1 {
switch pathComponents[1] { switch pathComponents[1] {
@ -540,15 +564,24 @@ WHERE p.id = ?1`
return return
case "": case "":
// view markdown file with a fancy HTML rendering step // view markdown file with a fancy HTML rendering step
case "md": case "md", "org":
if lang != "Markdown" { if lang != "Markdown" && lang != "Org" {
http.Redirect(w, r, "/paste/"+id, http.StatusTemporaryRedirect) http.Redirect(w, r, "/paste/"+id, http.StatusTemporaryRedirect)
return return
} }
title, ok := strings.CutPrefix(strings.Split(strings.TrimSpace(data), "\n")[0], "#") title := fname
if !ok { if lang == "Markdown" {
title = fname 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 { err = s.tmpls.ExecuteTemplate(w, "fancypost.html", struct {

View file

@ -25,6 +25,10 @@
font-size: 0.875em; font-size: 0.875em;
} }
} }
img {
max-width: 100%;
}
</style> </style>
</head> </head>
<body id="top"> <body id="top">

1
go.mod
View file

@ -6,6 +6,7 @@ require (
github.com/go-enry/go-enry/v2 v2.8.4 github.com/go-enry/go-enry/v2 v2.8.4
github.com/google/uuid v1.3.1 github.com/google/uuid v1.3.1
github.com/microcosm-cc/bluemonday v1.0.25 github.com/microcosm-cc/bluemonday v1.0.25
github.com/niklasfasching/go-org v1.7.0
github.com/russross/blackfriday v1.6.0 github.com/russross/blackfriday v1.6.0
modernc.org/sqlite v1.25.0 modernc.org/sqlite v1.25.0
tailscale.com v1.48.2 tailscale.com v1.48.2

2
go.sum
View file

@ -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 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 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= 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.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 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc=
github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=

View file

@ -58,12 +58,12 @@ schema = 3
[mod."github.com/coreos/go-iptables"] [mod."github.com/coreos/go-iptables"]
version = "v0.6.0" version = "v0.6.0"
hash = "sha256-hB+9XsybJuEFozAXo+G8+2qMMZGoqBbMmSwmcNJgmSU=" hash = "sha256-hB+9XsybJuEFozAXo+G8+2qMMZGoqBbMmSwmcNJgmSU="
[mod."github.com/dustin/go-humanize"]
version = "v1.0.1"
hash = "sha256-yuvxYYngpfVkUg9yAmG99IUVmADTQA0tMbBXe0Fq0Mc="
[mod."github.com/dblohm7/wingoes"] [mod."github.com/dblohm7/wingoes"]
version = "v0.0.0-20230803162905-5c6286bb8c6e" version = "v0.0.0-20230803162905-5c6286bb8c6e"
hash = "sha256-YPZP7Hq96I9+3gHyLp0sTsk0IN2wWSJXGHh+kJNRgAk=" hash = "sha256-YPZP7Hq96I9+3gHyLp0sTsk0IN2wWSJXGHh+kJNRgAk="
[mod."github.com/dustin/go-humanize"]
version = "v1.0.1"
hash = "sha256-yuvxYYngpfVkUg9yAmG99IUVmADTQA0tMbBXe0Fq0Mc="
[mod."github.com/fxamacker/cbor/v2"] [mod."github.com/fxamacker/cbor/v2"]
version = "v2.4.0" version = "v2.4.0"
hash = "sha256-LwOrfDN5fx/R2lgaD90RIu2NxcJDpyqYEURLc9Kgxgw=" hash = "sha256-LwOrfDN5fx/R2lgaD90RIu2NxcJDpyqYEURLc9Kgxgw="
@ -113,8 +113,8 @@ schema = 3
version = "v1.1.1-0.20230202152459-5c7d0dd6ab86" version = "v1.1.1-0.20230202152459-5c7d0dd6ab86"
hash = "sha256-dgyrLXuM55z8FAoUjyt5TDlzim6HfphWo5wx1/DHLwE=" hash = "sha256-dgyrLXuM55z8FAoUjyt5TDlzim6HfphWo5wx1/DHLwE="
[mod."github.com/jsimonetti/rtnetlink"] [mod."github.com/jsimonetti/rtnetlink"]
version = "v1.1.2-0.20220408201609-d380b505068b" version = "v1.3.2"
hash = "sha256-xHfsLwbycxNNyFI9zTfSMsb2T35BKEWyW6Fmk1tDWM4=" hash = "sha256-clvnQA5M4NHgv03tGDmdFmcfSjoqeZC3Gfy30rtFECI="
[mod."github.com/kballard/go-shellquote"] [mod."github.com/kballard/go-shellquote"]
version = "v0.0.0-20180428030007-95032a82bc51" version = "v0.0.0-20180428030007-95032a82bc51"
hash = "sha256-AOEdKETBMUC39ln6jBJ9NYdJWp++jV5lSbjNqG3dV+c=" hash = "sha256-AOEdKETBMUC39ln6jBJ9NYdJWp++jV5lSbjNqG3dV+c="
@ -125,8 +125,8 @@ schema = 3
version = "v0.0.0-20200729010619-da482cc4850a" version = "v0.0.0-20200729010619-da482cc4850a"
hash = "sha256-lnr9r/KNv4EeeNohFImC3Vd5E9nJ0N+4ZZ0VHFjwHps=" hash = "sha256-lnr9r/KNv4EeeNohFImC3Vd5E9nJ0N+4ZZ0VHFjwHps="
[mod."github.com/mattn/go-isatty"] [mod."github.com/mattn/go-isatty"]
version = "v0.0.16" version = "v0.0.18"
hash = "sha256-YMaPZvShDfA98vqw1+zWWl7M1IT4nHPGBrAt7kHo8Iw=" hash = "sha256-QpIn0DSggtBn2ocyj0RlXDKLK5F5KZG1/ogzrqBCjF8="
[mod."github.com/mdlayher/genetlink"] [mod."github.com/mdlayher/genetlink"]
version = "v1.3.2" version = "v1.3.2"
hash = "sha256-pgwXkyDY1dlB8tmV1lQ0Bz/2g0zmJOyXvQjacACy924=" hash = "sha256-pgwXkyDY1dlB8tmV1lQ0Bz/2g0zmJOyXvQjacACy924="
@ -148,6 +148,9 @@ schema = 3
[mod."github.com/mitchellh/go-ps"] [mod."github.com/mitchellh/go-ps"]
version = "v1.0.0" version = "v1.0.0"
hash = "sha256-HzxVHNLHZpnsBuPcub0G+9jjDcDOsxM/6wifbsxf7EY=" hash = "sha256-HzxVHNLHZpnsBuPcub0G+9jjDcDOsxM/6wifbsxf7EY="
[mod."github.com/niklasfasching/go-org"]
version = "v1.7.0"
hash = "sha256-i3NdcfER5JSIJv3GIJxeNJJpOyxiyxQKPZPpK2teQt4="
[mod."github.com/pierrec/lz4/v4"] [mod."github.com/pierrec/lz4/v4"]
version = "v4.1.17" version = "v4.1.17"
hash = "sha256-36L+GNhRrHBCyhbHCqweCk5rfmggdRtHqH6g5m1ViQI=" hash = "sha256-36L+GNhRrHBCyhbHCqweCk5rfmggdRtHqH6g5m1ViQI="