From 7d307ad5675d63bc23ef22468f479e8c37d25266 Mon Sep 17 00:00:00 2001 From: Christoph Herb <52382992+chrishrb@users.noreply.github.com> Date: Wed, 30 Oct 2024 16:53:33 +0100 Subject: [PATCH 1/2] 1 Fix template folder missing in binary bug --- defaults/embed.go | 9 + .../static}/css/github-markdown-dark.css | 23 ++- .../static}/css/github-markdown-light.css | 182 ++++++++++-------- .../static}/images/favicon.ico | Bin .../templates}/alert/caution.html | 0 .../templates}/alert/important.html | 0 .../templates}/alert/note.html | 0 .../templates}/alert/tip.html | 0 .../templates}/alert/warning.html | 0 {templates => defaults/templates}/layout.html | 0 pkg/parser.go | 3 +- pkg/webserver.go | 6 +- 12 files changed, 132 insertions(+), 91 deletions(-) create mode 100644 defaults/embed.go rename {static => defaults/static}/css/github-markdown-dark.css (98%) rename {static => defaults/static}/css/github-markdown-light.css (85%) rename {static => defaults/static}/images/favicon.ico (100%) rename {templates => defaults/templates}/alert/caution.html (100%) rename {templates => defaults/templates}/alert/important.html (100%) rename {templates => defaults/templates}/alert/note.html (100%) rename {templates => defaults/templates}/alert/tip.html (100%) rename {templates => defaults/templates}/alert/warning.html (100%) rename {templates => defaults/templates}/layout.html (100%) diff --git a/defaults/embed.go b/defaults/embed.go new file mode 100644 index 0000000..856e036 --- /dev/null +++ b/defaults/embed.go @@ -0,0 +1,9 @@ +package defaults + +import "embed" + +//go:embed templates +var Templates embed.FS + +//go:embed static +var StaticFiles embed.FS diff --git a/static/css/github-markdown-dark.css b/defaults/static/css/github-markdown-dark.css similarity index 98% rename from static/css/github-markdown-dark.css rename to defaults/static/css/github-markdown-dark.css index b5ab566..9205103 100644 --- a/static/css/github-markdown-dark.css +++ b/defaults/static/css/github-markdown-dark.css @@ -1,17 +1,22 @@ /* custom dark */ -.container { - max-width: 896px; - margin: 0 auto; +@media (min-width: 940px) { + .container { + max-width: 896px; + margin: 0 auto; + } + + .container-inner { + border-width: 1px; + border-color: #30363d; + border-style: solid; + border-radius: 6px; + margin-top: 20px; + margin-bottom: 6px; + } } .container-inner { padding: 32px; - border-width: 1px; - border-color: #30363d; - border-style: solid; - border-radius: 6px; - margin-top: 20px; - margin-bottom: 6px; } .footer { diff --git a/static/css/github-markdown-light.css b/defaults/static/css/github-markdown-light.css similarity index 85% rename from static/css/github-markdown-light.css rename to defaults/static/css/github-markdown-light.css index 05b6bf8..393e005 100644 --- a/static/css/github-markdown-light.css +++ b/defaults/static/css/github-markdown-light.css @@ -1,17 +1,22 @@ /* custom light */ -.container { - max-width: 896px; - margin: 0 auto; +@media (min-width: 940px) { + .container { + max-width: 896px; + margin: 0 auto; + } + + .container-inner { + border-width: 1px; + border-color: #d0d7de; + border-style: solid; + border-radius: 6px; + margin-top: 20px; + margin-bottom: 6px; + } } .container-inner { padding: 32px; - border-width: 1px; - border-color: #d0d7de; - border-style: solid; - border-radius: 6px; - margin-top: 20px; - margin-bottom: 6px; } .footer { @@ -28,7 +33,8 @@ margin: 0; color: #1f2328; background-color: #ffffff; - font-family: -apple-system,BlinkMacSystemFont,"Segoe UI","Noto Sans",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"; + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans', + Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji'; font-size: 16px; line-height: 1.5; word-wrap: break-word; @@ -92,9 +98,9 @@ } .markdown-body h1 { - margin: .67em 0; + margin: 0.67em 0; font-weight: 600; - padding-bottom: .3em; + padding-bottom: 0.3em; font-size: 2em; border-bottom: 1px solid #d1d9e0b3; } @@ -147,7 +153,7 @@ overflow: hidden; background: transparent; border-bottom: 1px solid #d1d9e0b3; - height: .25em; + height: 0.25em; padding: 0; margin: 1.5rem 0; background-color: #d1d9e0; @@ -163,33 +169,33 @@ line-height: inherit; } -.markdown-body [type=button], -.markdown-body [type=reset], -.markdown-body [type=submit] { +.markdown-body [type='button'], +.markdown-body [type='reset'], +.markdown-body [type='submit'] { -webkit-appearance: button; appearance: button; } -.markdown-body [type=checkbox], -.markdown-body [type=radio] { +.markdown-body [type='checkbox'], +.markdown-body [type='radio'] { box-sizing: border-box; padding: 0; } -.markdown-body [type=number]::-webkit-inner-spin-button, -.markdown-body [type=number]::-webkit-outer-spin-button { +.markdown-body [type='number']::-webkit-inner-spin-button, +.markdown-body [type='number']::-webkit-outer-spin-button { height: auto; } -.markdown-body [type=search]::-webkit-search-cancel-button, -.markdown-body [type=search]::-webkit-search-decoration { +.markdown-body [type='search']::-webkit-search-cancel-button, +.markdown-body [type='search']::-webkit-search-decoration { -webkit-appearance: none; appearance: none; } .markdown-body ::-webkit-input-placeholder { color: inherit; - opacity: .54; + opacity: 0.54; } .markdown-body ::-webkit-file-upload-button { @@ -209,13 +215,13 @@ .markdown-body hr::before { display: table; - content: ""; + content: ''; } .markdown-body hr::after { display: table; clear: both; - content: ""; + content: ''; } .markdown-body table { @@ -237,25 +243,25 @@ } .markdown-body a:focus, -.markdown-body [role=button]:focus, -.markdown-body input[type=radio]:focus, -.markdown-body input[type=checkbox]:focus { +.markdown-body [role='button']:focus, +.markdown-body input[type='radio']:focus, +.markdown-body input[type='checkbox']:focus { outline: 2px solid #0969da; outline-offset: -2px; box-shadow: none; } .markdown-body a:focus:not(:focus-visible), -.markdown-body [role=button]:focus:not(:focus-visible), -.markdown-body input[type=radio]:focus:not(:focus-visible), -.markdown-body input[type=checkbox]:focus:not(:focus-visible) { +.markdown-body [role='button']:focus:not(:focus-visible), +.markdown-body input[type='radio']:focus:not(:focus-visible), +.markdown-body input[type='checkbox']:focus:not(:focus-visible) { outline: solid 1px transparent; } .markdown-body a:focus-visible, -.markdown-body [role=button]:focus-visible, -.markdown-body input[type=radio]:focus-visible, -.markdown-body input[type=checkbox]:focus-visible { +.markdown-body [role='button']:focus-visible, +.markdown-body input[type='radio']:focus-visible, +.markdown-body input[type='checkbox']:focus-visible { outline: 2px solid #0969da; outline-offset: -2px; box-shadow: none; @@ -263,17 +269,24 @@ .markdown-body a:not([class]):focus, .markdown-body a:not([class]):focus-visible, -.markdown-body input[type=radio]:focus, -.markdown-body input[type=radio]:focus-visible, -.markdown-body input[type=checkbox]:focus, -.markdown-body input[type=checkbox]:focus-visible { +.markdown-body input[type='radio']:focus, +.markdown-body input[type='radio']:focus-visible, +.markdown-body input[type='checkbox']:focus, +.markdown-body input[type='checkbox']:focus-visible { outline-offset: 0; } .markdown-body kbd { display: inline-block; padding: 0.25rem; - font: 11px ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace; + font: + 11px ui-monospace, + SFMono-Regular, + SF Mono, + Menlo, + Consolas, + Liberation Mono, + monospace; line-height: 10px; color: #1f2328; vertical-align: middle; @@ -298,7 +311,7 @@ .markdown-body h2 { font-weight: 600; - padding-bottom: .3em; + padding-bottom: 0.3em; font-size: 1.5em; border-bottom: 1px solid #d1d9e0b3; } @@ -315,12 +328,12 @@ .markdown-body h5 { font-weight: 600; - font-size: .875em; + font-size: 0.875em; } .markdown-body h6 { font-weight: 600; - font-size: .85em; + font-size: 0.85em; color: #59636e; } @@ -333,7 +346,7 @@ margin: 0; padding: 0 1em; color: #59636e; - border-left: .25em solid #d1d9e0; + border-left: 0.25em solid #d1d9e0; } .markdown-body ul, @@ -362,14 +375,28 @@ .markdown-body tt, .markdown-body code, .markdown-body samp { - font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace; + font-family: + ui-monospace, + SFMono-Regular, + SF Mono, + Menlo, + Consolas, + Liberation Mono, + monospace; font-size: 12px; } .markdown-body pre { margin-top: 0; margin-bottom: 0; - font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas, Liberation Mono, monospace; + font-family: + ui-monospace, + SFMono-Regular, + SF Mono, + Menlo, + Consolas, + Liberation Mono, + monospace; font-size: 12px; word-wrap: normal; } @@ -394,13 +421,13 @@ .markdown-body::before { display: table; - content: ""; + content: ''; } .markdown-body::after { display: table; clear: both; - content: ""; + content: ''; } .markdown-body>*:first-child { @@ -443,11 +470,11 @@ margin-bottom: 1rem; } -.markdown-body blockquote>:first-child { +.markdown-body blockquote> :first-child { margin-top: 0; } -.markdown-body blockquote>:last-child { +.markdown-body blockquote> :last-child { margin-bottom: 0; } @@ -492,7 +519,7 @@ .markdown-body h5 code, .markdown-body h6 tt, .markdown-body h6 code { - padding: 0 .2em; + padding: 0 0.2em; font-size: inherit; } @@ -526,23 +553,23 @@ list-style-type: none; } -.markdown-body ol[type="a s"] { +.markdown-body ol[type='a s'] { list-style-type: lower-alpha; } -.markdown-body ol[type="A s"] { +.markdown-body ol[type='A s'] { list-style-type: upper-alpha; } -.markdown-body ol[type="i s"] { +.markdown-body ol[type='i s'] { list-style-type: lower-roman; } -.markdown-body ol[type="I s"] { +.markdown-body ol[type='I s'] { list-style-type: upper-roman; } -.markdown-body ol[type="1"] { +.markdown-body ol[type='1'] { list-style-type: decimal; } @@ -563,7 +590,7 @@ } .markdown-body li+li { - margin-top: .25em; + margin-top: 0.25em; } .markdown-body dl { @@ -593,7 +620,7 @@ border: 1px solid #d1d9e0; } -.markdown-body table td>:last-child { +.markdown-body table td> :last-child { margin-bottom: 0; } @@ -610,11 +637,11 @@ background-color: transparent; } -.markdown-body img[align=right] { +.markdown-body img[align='right'] { padding-left: 20px; } -.markdown-body img[align=left] { +.markdown-body img[align='left'] { padding-right: 20px; } @@ -714,7 +741,7 @@ .markdown-body code, .markdown-body tt { - padding: .2em .4em; + padding: 0.2em 0.4em; margin: 0; font-size: 85%; white-space: break-spaces; @@ -809,11 +836,11 @@ } .markdown-body [data-footnote-ref]::before { - content: "["; + content: '['; } .markdown-body [data-footnote-ref]::after { - content: "]"; + content: ']'; } .markdown-body .footnotes { @@ -838,12 +865,12 @@ .markdown-body .footnotes li:target::before { position: absolute; - top: calc(0.5rem*-1); - right: calc(0.5rem*-1); - bottom: calc(0.5rem*-1); - left: calc(1.5rem*-1); + top: calc(0.5rem * -1); + right: calc(0.5rem * -1); + bottom: calc(0.5rem * -1); + left: calc(1.5rem * -1); pointer-events: none; - content: ""; + content: ''; border: 2px solid #0969da; border-radius: 6px; } @@ -976,8 +1003,8 @@ color: #0a3069; } -.markdown-body [role=button]:focus:not(:focus-visible), -.markdown-body [role=tabpanel][tabindex="0"]:focus:not(:focus-visible), +.markdown-body [role='button']:focus:not(:focus-visible), +.markdown-body [role='tabpanel'][tabindex='0']:focus:not(:focus-visible), .markdown-body button:focus:not(:focus-visible), .markdown-body summary:focus:not(:focus-visible), .markdown-body a:focus:not(:focus-visible) { @@ -985,7 +1012,7 @@ box-shadow: none; } -.markdown-body [tabindex="0"]:focus:not(:focus-visible), +.markdown-body [tabindex='0']:focus:not(:focus-visible), .markdown-body details-dialog:focus:not(:focus-visible) { outline: none; } @@ -993,7 +1020,7 @@ .markdown-body g-emoji { display: inline-block; min-width: 1ch; - font-family: "Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol"; + font-family: 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 1em; font-style: normal !important; font-weight: 400; @@ -1027,16 +1054,16 @@ } .markdown-body .task-list-item-checkbox { - margin: 0 .2em .25em -1.4em; + margin: 0 0.2em 0.25em -1.4em; vertical-align: middle; } .markdown-body ul:dir(rtl) .task-list-item-checkbox { - margin: 0 -1.6em .25em .2em; + margin: 0 -1.6em 0.25em 0.2em; } .markdown-body ol:dir(rtl) .task-list-item-checkbox { - margin: 0 -1.6em .25em .2em; + margin: 0 -1.6em 0.25em 0.2em; } .markdown-body .contains-task-list:hover .task-list-item-convert-container, @@ -1056,14 +1083,14 @@ padding: 0.5rem 1rem; margin-bottom: 1rem; color: inherit; - border-left: .25em solid #d1d9e0; + border-left: 0.25em solid #d1d9e0; } -.markdown-body .markdown-alert>:first-child { +.markdown-body .markdown-alert> :first-child { margin-top: 0; } -.markdown-body .markdown-alert>:last-child { +.markdown-body .markdown-alert> :last-child { margin-bottom: 0; } @@ -1117,4 +1144,3 @@ .markdown-body>*:first-child>.heading-element:first-child { margin-top: 0 !important; } - diff --git a/static/images/favicon.ico b/defaults/static/images/favicon.ico similarity index 100% rename from static/images/favicon.ico rename to defaults/static/images/favicon.ico diff --git a/templates/alert/caution.html b/defaults/templates/alert/caution.html similarity index 100% rename from templates/alert/caution.html rename to defaults/templates/alert/caution.html diff --git a/templates/alert/important.html b/defaults/templates/alert/important.html similarity index 100% rename from templates/alert/important.html rename to defaults/templates/alert/important.html diff --git a/templates/alert/note.html b/defaults/templates/alert/note.html similarity index 100% rename from templates/alert/note.html rename to defaults/templates/alert/note.html diff --git a/templates/alert/tip.html b/defaults/templates/alert/tip.html similarity index 100% rename from templates/alert/tip.html rename to defaults/templates/alert/tip.html diff --git a/templates/alert/warning.html b/defaults/templates/alert/warning.html similarity index 100% rename from templates/alert/warning.html rename to defaults/templates/alert/warning.html diff --git a/templates/layout.html b/defaults/templates/layout.html similarity index 100% rename from templates/layout.html rename to defaults/templates/layout.html diff --git a/pkg/parser.go b/pkg/parser.go index 435a03b..f0b64d8 100644 --- a/pkg/parser.go +++ b/pkg/parser.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/alecthomas/chroma/v2/quick" + "github.com/chrishrb/go-grip/defaults" "github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown/ast" "github.com/gomarkdown/markdown/html" @@ -187,7 +188,7 @@ func renderHookListItem(w io.Writer, node ast.Node, entering bool) (ast.WalkStat func createBlockquoteStart(alert string) (string, error) { lp := filepath.Join("templates/alert", fmt.Sprintf("%s.html", alert)) - tmpl, err := template.ParseFiles(lp) + tmpl, err := template.ParseFS(defaults.Templates, lp) if err != nil { return "", err } diff --git a/pkg/webserver.go b/pkg/webserver.go index b237aff..c23866e 100644 --- a/pkg/webserver.go +++ b/pkg/webserver.go @@ -7,11 +7,11 @@ import ( "log" "net/http" "net/url" - "path/filepath" "regexp" "text/template" "github.com/aarol/reload" + "github.com/chrishrb/go-grip/defaults" ) type htmlStruct struct { @@ -25,6 +25,7 @@ func (client *Client) Serve(file string) error { dir := http.Dir("./") chttp := http.NewServeMux() + chttp.Handle("/static/", http.FileServer(http.FS(defaults.StaticFiles))) chttp.Handle("/", http.FileServer(dir)) // Regex for markdown @@ -103,8 +104,7 @@ func readToString(dir http.Dir, filename string) ([]byte, error) { func serveTemplate(w http.ResponseWriter, html htmlStruct) error { w.Header().Set("Content-Type", "text/html") - lp := filepath.Join("templates", "layout.html") - tmpl, err := template.ParseFiles(lp) + tmpl, err := template.ParseFS(defaults.Templates, "templates/layout.html") if err != nil { return err } From 132ad1f14cecd88429abd96fe2212cd0b545ce65 Mon Sep 17 00:00:00 2001 From: Christoph Herb <52382992+chrishrb@users.noreply.github.com> Date: Wed, 30 Oct 2024 17:26:43 +0100 Subject: [PATCH 2/2] Fix path issue --- pkg/webserver.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/webserver.go b/pkg/webserver.go index c23866e..f08f133 100644 --- a/pkg/webserver.go +++ b/pkg/webserver.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "net/url" + "path/filepath" "regexp" "text/template" @@ -20,10 +21,13 @@ type htmlStruct struct { } func (client *Client) Serve(file string) error { - reload := reload.New("./") + directory := filepath.Dir(file) + filename := filepath.Base(file) + + reload := reload.New(directory) reload.Log = log.New(io.Discard, "", 0) - dir := http.Dir("./") + dir := http.Dir(directory) chttp := http.NewServeMux() chttp.Handle("/static/", http.FileServer(http.FS(defaults.StaticFiles))) chttp.Handle("/", http.FileServer(dir)) @@ -70,7 +74,7 @@ func (client *Client) Serve(file string) error { addr, _ = url.JoinPath(addr, readme) } } else { - addr, _ = url.JoinPath(addr, file) + addr, _ = url.JoinPath(addr, filename) } fmt.Printf("🚀 Starting server: %s\n", addr)