Skip to content

Commit

Permalink
Adds ability to set a custom markdown theme by specifying the path to…
Browse files Browse the repository at this point in the history
… a CSS file instead of the name of a built-in theme
  • Loading branch information
tantalic committed Jan 31, 2017
1 parent 019d974 commit 0631273
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 91 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Usage: `servemd [OPTIONS] [DIR]`
| `X_ROBOTS_TAG` | `-r --x-robots-tag` | Sets a `X-Robots-Tag` header. Example: `"noindex, nofollow"` | None |
| `DOCUMENT_EXTENSION` | `-e, --extension` | Extension used for markdown files | `.md` |
| `DIRECTORY_INDEX` | `-i, --index` | Filename (without extension) to use for directory indexes | `index` |
| `MARKDOWN_THEME` | `-m, --markdown-theme` | Theme to use for styling markdown. Values: *clean*, *github*, *developer* | `clean` |
| `MARKDOWN_THEME` | `-m, --markdown-theme` | Theme to use for styling markdown. Can be one of the following built-in themes: *clean*, *github*, *developer* or the path to a custom CSS file to include. | `clean` |
| `CODE_THEME` | `-c, --code-theme` | Syntax highlighting theme (powered by [highlight.js][highlightjs]) | None |


Expand Down
2 changes: 1 addition & 1 deletion assets/templates/document.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{{if .MarkdownTheme}}
<!-- Markdown Theme -->
<link rel="stylesheet" type="text/css" href="/assets/themes/{{.MarkdownTheme}}.css">
<link rel="stylesheet" type="text/css" href="{{.MarkdownTheme}}">
{{end}}

{{if .CodeTheme}}
Expand Down
175 changes: 89 additions & 86 deletions bindata_assetfs.go

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

const (
Version = "0.4.0"
Version = "0.4.0"
DefaultTheme = "clean"
)

func main() {
Expand Down Expand Up @@ -67,7 +68,7 @@ func main() {
markdownTheme = app.String(cli.StringOpt{
Name: "m markdown-theme",
Desc: "Theme to use for styling markdown html",
Value: "clean",
Value: DefaultTheme,
EnvVar: "MARKDOWN_THEME",
})
codeTheme = app.String(cli.StringOpt{
Expand All @@ -86,12 +87,18 @@ func main() {
}
http.HandleFunc("/assets/", headerMiddleware(staticAssetHandlerFunc))

// Setup the markdown theme (may be custom or bundled)
themePath, themeHandler := theme(*markdownTheme)
if themeHandler != nil {
http.HandleFunc(themePath, themeHandler)
}

// Markdown File Handler
markdownHandlerFunc := markdownHandleFunc(MarkdownHandlerOptions{
DocRoot: *dir,
DocExtension: *extension,
DirIndex: *index,
MarkdownTheme: *markdownTheme,
MarkdownTheme: themePath,
CodeTheme: *codeTheme,
})
markdownHandlerFunc = headerMiddleware(markdownHandlerFunc)
Expand Down
49 changes: 49 additions & 0 deletions theme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
)

func theme(value string) (string, http.HandlerFunc) {
// Check if the value is a path to a CSS file
if filepath.Ext(value) == ".css" {
cssPath, err := filepath.Abs(value)
if err != nil {
log.Printf("Error finding custom theme: %s: %s. Falling back to: %s.", value, err.Error(), DefaultTheme)
return themePath(DefaultTheme), nil
}

if stat, err := os.Stat(cssPath); err == nil && !stat.IsDir() {
// Add handler to serve content
handler := func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, cssPath)
}
return themePath("custom"), handler
}

log.Printf("Error finding custom theme: %s. Falling back to: %s.", value, DefaultTheme)
return themePath(DefaultTheme), nil
}

// If the value is not a path, check that it is a valid bundled theme
localPath := themeAssetPath(value)
_, err := Asset(localPath)
if err != nil {
log.Printf("Invalid theme selected: %s. Falling back to: %s.", value, DefaultTheme)
return themePath(DefaultTheme), nil
}

return themePath(value), nil
}

func themePath(value string) string {
return fmt.Sprintf("/assets/themes/%s.css", value)
}

func themeAssetPath(value string) string {
return fmt.Sprintf("assets/themes/%s.css", value)
}

0 comments on commit 0631273

Please sign in to comment.