Skip to content

Commit

Permalink
Merge pull request #64 from solworktech/customisable-themes
Browse files Browse the repository at this point in the history
Support custom themes (by passing a JSON file).
  • Loading branch information
jessp01 authored Feb 10, 2025
2 parents 3aa52c0 + ed10c42 commit 28508de
Show file tree
Hide file tree
Showing 7 changed files with 559 additions and 67 deletions.
48 changes: 24 additions & 24 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
# For more information see:
# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

---

name: Go

Expand All @@ -14,26 +17,23 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Build
run: go build -v
- name: Test
run: |
set -e
go install github.com/go-critic/go-critic/cmd/gocritic@latest
# go install golang.org/x/tools/cmd/goimports@latest
go install golang.org/x/lint/golint@latest
go install github.com/gordonklaus/ineffassign@latest
pip install pre-commit
pre-commit install
pre-commit run --all-files
go test -v
#- name: Test
#run: go test -v
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Build
run: go build -v
- name: Test
run: |
set -e
go install github.com/go-critic/go-critic/cmd/gocritic@latest
# go install golang.org/x/tools/cmd/goimports@latest
go install golang.org/x/lint/golint@latest
go install github.com/gordonklaus/ineffassign@latest
pip install pre-commit
pre-commit install
pre-commit run --all-files
go test -v
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ Both of the above are documented at [Go Docs](http://godocs.org).

## Features

- Syntax highlighting (for code blocks)
- Dark and light themes
- Pagination control (using horizontal lines - especially useful for presentations)
- Page Footer (consisting of author, title and page number)
- Support of non-Latin charsets and multiple fonts
- [Syntax highlighting (for code blocks)](#syntax-highlighting)
- [Dark and light themes](#custom-themes)
- [Customised themes (by passing a JSON file to `md2pdf`)](#custom-themes)
- [Support of non-Latin charsets and multiple fonts](#using-non-ascii-glyphsfonts)
- [Pagination control (using horizontal lines - especially useful for presentations)](#additional-options)
- [Page Footer (consisting of author, title and page number)](#additional-options)

## Supported Markdown elements

Expand Down Expand Up @@ -61,8 +62,6 @@ This is a planned fix; [see here](/~https://github.com/mandolyte/mdtopdf/issues/1)

7. Tables are supported, but no attempt is made to ensure fit. You can, however, change the font size and spacing to make it smaller. See example.



## Installation

You can obtain the pre-built `md2pdf` binary for your OS and arch [here](/~https://github.com/mandolyte/mdtopdf/releases);
Expand All @@ -83,6 +82,13 @@ $ go install github.com/mandolyte/mdtopdf/cmd/md2pdf@latest

For examples, see `testdata/Markdown Documentation - Syntax.text` and `testdata/Markdown Documentation - Syntax.pdf`

## Custom themes

`md2pdf` supports both light and dark themes out of the box (use `--theme light` or `--theme dark` - no config required).

However, if you wish to customise the font faces, sizes and colours, you can use the JSONs in
[custom_themes](./custom_themes) as a starting point. Edit to your liking and pass `--theme /path/to/json` to `md2pdf`

## Quick start

In the `cmd` folder is an example using the package. It demonstrates
Expand Down
41 changes: 26 additions & 15 deletions cmd/md2pdf/md2pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var author = flag.String("author", "", "Author; used if -footer is passed")
var unicodeSupport = flag.String("unicode-encoding", "", "e.g 'cp1251'")
var fontFile = flag.String("font-file", "", "path to font file to use")
var fontName = flag.String("font-name", "", "Font name ID; e.g 'Helvetica-1251'")
var themeArg = flag.String("theme", "light", "[light|dark]")
var themeArg = flag.String("theme", "light", "[light | dark | /path/to/custom/theme.json]")
var hrAsNewPage = flag.Bool("new-page-on-hr", false, "Interpret HR as a new page; useful for presentations")
var printFooter = flag.Bool("with-footer", false, "Print doc footer (author title page number)")
var pageSize = flag.String("page-size", "A4", "[A3 | A4 | A5]")
Expand Down Expand Up @@ -140,38 +140,49 @@ func main() {
}

theme := mdtopdf.LIGHT
textColor := mdtopdf.Colorlookup("black")
fillColor := mdtopdf.Colorlookup("white")
backgroundColor := "white"
themeFile := ""
if *themeArg == "dark" {
theme = mdtopdf.DARK
backgroundColor = "black"
textColor = mdtopdf.Colorlookup("darkgray")
fillColor = mdtopdf.Colorlookup("black")
} else if _, err := os.Stat(*themeArg); err == nil {
theme = mdtopdf.CUSTOM
themeFile = *themeArg
}

pf := mdtopdf.NewPdfRenderer(*orientation, *pageSize, *output, *logFile, opts, theme)
params := mdtopdf.PdfRendererParams{
Orientation: *orientation,
Papersz: *pageSize,
PdfFile: *output,
TracerFile: *logFile,
Opts: opts,
Theme: theme,
CustomThemeFile: themeFile,
FontFile: *fontFile,
FontName: *fontName,
}

pf := mdtopdf.NewPdfRenderer(params)
if inputBaseURL != "" {
pf.InputBaseURL = inputBaseURL
}
pf.Pdf.SetSubject(*title, true)
pf.Pdf.SetTitle(*title, true)
pf.BackgroundColor = mdtopdf.Colorlookup(backgroundColor)
pf.Extensions = parser.NoIntraEmphasis | parser.Tables | parser.FencedCode | parser.Autolink | parser.Strikethrough | parser.SpaceHeadings | parser.HeadingIDs | parser.BackslashLineBreak | parser.DefinitionLists

if *fontFile != "" && *fontName != "" {
pf.Pdf.AddFont(*fontName, "", *fontFile)
pf.Pdf.SetFont(*fontName, "", 12)
pf.Normal = mdtopdf.Styler{Font: *fontName, Style: "",
Size: 12, Spacing: 2,
FillColor: fillColor,
TextColor: textColor}
pf.Normal = mdtopdf.Styler{
Font: *fontName,
Style: "",
Size: 12, Spacing: 2,
TextColor: pf.Normal.TextColor,
}

}

if *printFooter {
pf.Pdf.SetFooterFunc(func() {
color := mdtopdf.Colorlookup(backgroundColor)
pf.Pdf.SetFillColor(color.Red, color.Green, color.Blue)
pf.Pdf.SetFillColor(pf.BackgroundColor.Red, pf.BackgroundColor.Green, pf.BackgroundColor.Blue)
// Position at 1.5 cm from bottom
pf.Pdf.SetY(-15)
// Arial italic 8
Expand Down
217 changes: 217 additions & 0 deletions custom_themes/dark_theme.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
{
"Normal": {
"Font": "Arial",
"Style": "",
"Size": 12,
"Spacing": 2,
"TextColor": {
"Red": 255,
"Green": 255,
"Blue": 255
},
"FillColor": {
"Red": 0,
"Green": 0,
"Blue": 0
}
},
"Link": {
"Font": "Arial",
"Style": "b",
"Size": 12,
"Spacing": 2,
"TextColor": {
"Red": 100,
"Green": 149,
"Blue": 237
},
"FillColor": {
"Red": 0,
"Green": 0,
"Blue": 0
}
},
"Backtick": {
"Font": "Times",
"Style": "",
"Size": 12,
"Spacing": 2,
"TextColor": {
"Red": 211,
"Green": 211,
"Blue": 211
},
"FillColor": {
"Red": 32,
"Green": 35,
"Blue": 37
}
},
"Blockquote": {
"Font": "Arial",
"Style": "i",
"Size": 12,
"Spacing": 2,
"TextColor": {
"Red": 169,
"Green": 169,
"Blue": 169
},
"FillColor": {
"Red": 0,
"Green": 0,
"Blue": 0
}
},
"IndentValue": 0,
"H1": {
"Font": "Arial",
"Style": "b",
"Size": 24,
"Spacing": 5,
"TextColor": {
"Red": 169,
"Green": 169,
"Blue": 169
},
"FillColor": {
"Red": 0,
"Green": 0,
"Blue": 0
}
},
"H2": {
"Font": "Arial",
"Style": "b",
"Size": 21,
"Spacing": 5,
"TextColor": {
"Red": 169,
"Green": 169,
"Blue": 169
},
"FillColor": {
"Red": 0,
"Green": 0,
"Blue": 0
}
},
"H3": {
"Font": "Arial",
"Style": "b",
"Size": 20,
"Spacing": 5,
"TextColor": {
"Red": 169,
"Green": 169,
"Blue": 169
},
"FillColor": {
"Red": 0,
"Green": 0,
"Blue": 0
}
},
"H4": {
"Font": "Arial",
"Style": "b",
"Size": 18,
"Spacing": 5,
"TextColor": {
"Red": 169,
"Green": 169,
"Blue": 169
},
"FillColor": {
"Red": 0,
"Green": 0,
"Blue": 0
}
},
"H5": {
"Font": "Arial",
"Style": "b",
"Size": 16,
"Spacing": 5,
"TextColor": {
"Red": 169,
"Green": 169,
"Blue": 169
},
"FillColor": {
"Red": 0,
"Green": 0,
"Blue": 0
}
},
"H6": {
"Font": "Arial",
"Style": "b",
"Size": 14,
"Spacing": 5,
"TextColor": {
"Red": 169,
"Green": 169,
"Blue": 169
},
"FillColor": {
"Red": 0,
"Green": 0,
"Blue": 0
}
},
"THeader": {
"Font": "Arial",
"Style": "b",
"Size": 12,
"Spacing": 2,
"TextColor": {
"Red": 169,
"Green": 169,
"Blue": 169
},
"FillColor": {
"Red": 27,
"Green": 27,
"Blue": 27
}
},
"TBody": {
"Font": "Arial",
"Style": "",
"Size": 12,
"Spacing": 2,
"TextColor": {
"Red": 128,
"Green": 128,
"Blue": 128
},
"FillColor": {
"Red": 200,
"Green": 200,
"Blue": 200
}
},
"Code": {
"Font": "Times",
"Style": "",
"Size": 12,
"Spacing": 2,
"TextColor": {
"Red": 211,
"Green": 211,
"Blue": 211
},
"FillColor": {
"Red": 32,
"Green": 35,
"Blue": 37
}
},
"Theme": 3,
"BackgroundColor": {
"Red": 0,
"Green": 0,
"Blue": 0
}
}
Loading

0 comments on commit 28508de

Please sign in to comment.