Skip to content

Commit

Permalink
allows multiple css parameters and css code
Browse files Browse the repository at this point in the history
  • Loading branch information
kpym committed Jul 26, 2024
1 parent d2e0b9b commit e25402a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ gm (version: 0.18.3): a goldmark cli tool which is a thin wrapper around github.
-s, --serve Start serving local .md file(s). No html is saved.
--timeout int Timeout in seconds for stop serving if no (non static) request. Default is 0 (no timeout).
-c, --css string A css url or the theme name present in github.com/kpym/markdown-css. (default "github")
-c, --css stringArray A css content or url or the theme name present in github.com/kpym/markdown-css. Multiple values are allowed. (default [github])
-t, --title string The page title. If empty, search for <h1> in the resulting html.
--html string The html template (file or string).
-o, --out-dir string The build output folder (created if not already existing, not used when serving).
Expand Down
19 changes: 16 additions & 3 deletions gm_compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,27 @@ func compile(markdown []byte) (html []byte, err error) {
}
htmlStr := htmlBuf.String()

// combine the template and the resulting
var data = make(map[string]template.HTML)
// combine the template and the resulting html
var data = make(map[string]any)
if title != "" {
data["title"] = template.HTML(title)
} else {
data["title"] = template.HTML(getTitle(htmlStr))
}
data["css"] = template.HTML(css)
// the css can be either an url or a code
type cssType struct {
Url template.HTML
Code template.HTML
}
cssall := make([]cssType, len(css))
for i, c := range css {
if strings.HasPrefix(c, "<style>") {
cssall[i] = cssType{Code: template.HTML(c)}
} else {
cssall[i] = cssType{Url: template.HTML(c)}
}
}
data["css"] = cssall
data["html"] = template.HTML(htmlStr)
if liveupdate {
data["liveupdate"] = template.HTML("yes")
Expand Down
13 changes: 9 additions & 4 deletions gm_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var (
pages bool

// template flags
css string
css []string
title string
htmlshell string
liveupdate bool
Expand Down Expand Up @@ -110,7 +110,7 @@ func SetParameters() {
pflag.Lookup("serve").NoOptDefVal = "true"
pflag.IntVar(&timeout, "timeout", 0, "Timeout in seconds for stop serving if no (non static) request. Default is 0 (no timeout).")

pflag.StringVarP(&css, "css", "c", "github", "A css url or the theme name present in github.com/kpym/markdown-css.")
pflag.StringArrayVarP(&css, "css", "c", []string{"github"}, "A css content or url or the theme name present in github.com/kpym/markdown-css. Multiple values are allowed.")
pflag.StringVarP(&title, "title", "t", "", "The page title. If empty, search for <h1> in the resulting html.")
pflag.StringVar(&htmlshell, "html", "", "The html template (file or string).")

Expand Down Expand Up @@ -163,8 +163,13 @@ func SetParameters() {
}

// set the css
if css != "" && !strings.Contains(css, "/") && !strings.Contains(css, ".") {
css = "https://kpym.github.io/markdown-css/" + css + ".min.css"
for i, c := range css {
// if not empty and not containing '/' or '.' or '{' it should be a theme name
if c != "" && !strings.ContainsAny(c, "/.{") {
css[i] = "https://kpym.github.io/markdown-css/" + c + ".min.css"
} else if strings.Contains(c, "{") {
css[i] = "<style>" + c + "</style>"
}
}
// set the template
t, err := os.ReadFile(htmlshell)
Expand Down
11 changes: 8 additions & 3 deletions gm_template.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
_ "embed"
_ "embed"
)

// defaultHTMLTemplate is the default value for `html` flag
Expand All @@ -10,8 +10,13 @@ var defaultHTMLTemplate string = `<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
{{- with .css }}
<link rel="stylesheet" type="text/css" href="{{.}}">
{{- range .css }}
{{- with .Url }}
<link rel="stylesheet" type="text/css" href="{{.}}">
{{- end }}
{{- with .Code }}
{{.}}
{{- end }}
{{- end }}
{{- with .title }}
<title>{{.}}</title>
Expand Down

0 comments on commit e25402a

Please sign in to comment.