Skip to content

Commit

Permalink
Added an option to use roman numerals for the footnotes in toml
Browse files Browse the repository at this point in the history
Signed-off-by: Sandy <sandy@sandyuraz.com>
  • Loading branch information
thecsw committed Jun 14, 2022
1 parent 19acc11 commit 51d4cda
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 7 deletions.
4 changes: 3 additions & 1 deletion emilia/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type ProjectConfig struct {
Input string
// Output is the output format (defaulte ".html")
Output string
// Excuses is the list of relative paths to exclude from the project
// Excludes is the list of relative paths to exclude from the project
Exclude []string
}

Expand All @@ -50,6 +50,8 @@ type WebsiteConfig struct {
DescriptionLength int `toml:"description_length"`
// Normalize headings will shift heading levels if enabled
NormalizeHeadings bool `toml:"normalize_headings"`
// RomanFootnotes tells if we have to use roman numerals for footnotes
RomanFootnotes bool `toml:"roman_footnotes"`
}

// AuthorConfig is the author section of the config
Expand Down
4 changes: 2 additions & 2 deletions html/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ func addFootnotes(page *internals.Page) string {
for i, footnote := range page.Footnotes {
footnotes[i] = fmt.Sprintf(`
<div class="footnote" id="_footnotedef_%d">
<a href="#_footnoteref_%d">%d</a>
<a href="#_footnoteref_%d">%s</a>
%s
</div>
`,
i+1, i+1, i+1, processText(footnote))
i+1, i+1, footnoteLabel(i+1), processText(footnote))
}
return fmt.Sprintf(`
<div id="footnotes">
Expand Down
75 changes: 72 additions & 3 deletions html/utils.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package html

import (
"fmt"
"html"
"regexp"
"strconv"
"strings"
"unicode"

"github.com/thecsw/darkness/emilia"
"github.com/thecsw/darkness/internals"
)

Expand Down Expand Up @@ -55,9 +58,12 @@ func processText(text string) string {

//text = internals.MathRegexp.ReplaceAllString(text, `\($1\)`)

text = internals.FootnotePostProcessingRegexp.ReplaceAllString(text, `
<sup class="footnote">[<a id="_footnoteref_$1" class="footnote" href="#_footnotedef_$1" title="View footnote.">$1</a>]</sup>
`)
text = internals.FootnotePostProcessingRegexp.ReplaceAllStringFunc(text, func(what string) string {
num, _ := strconv.Atoi(strings.ReplaceAll(what, "!", ""))
return fmt.Sprintf(`
<sup class="footnote">[<a id="_footnoteref_%d" class="footnote" href="#_footnotedef_%d" title="View footnote.">%s</a>]</sup>
`, num, num, footnoteLabel(num))
})

return strings.TrimSpace(text)
}
Expand Down Expand Up @@ -104,3 +110,66 @@ func extractID(heading string) string {
}
return res
}

var (
numToRomanMap = map[int]string{
1: "I",
4: "IV",
5: "V",
9: "IX",
10: "X",
40: "XL",
50: "L",
90: "XC",
100: "C",
400: "CD",
500: "D",
900: "CM",
1000: "M",
}
romanNumOrder = []int{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
numToRomanSmall = map[int]string{
1: "I",
2: "II",
3: "III",
4: "IV",
5: "V",
6: "VI",
7: "VII",
8: "VIII",
9: "IX",
10: "X",
11: "XI",
12: "XII",
13: "XIII",
14: "XIV",
}
)

// numberToRoman converts an integer to a roman numeral
// adapted from https://pencilprogrammer.com/python-programs/convert-integer-to-roman-numerals/
func numberToRoman(num int) string {
if num <= 14 {
return numToRomanSmall[num]
}
res := ""
for _, v := range romanNumOrder {
if num != 0 {
quot := num / v
if quot != 0 {
for x := 0; x < quot; x++ {
res += numToRomanMap[v]
}
}
num %= v
}
}
return res
}

func footnoteLabel(num int) string {
if emilia.Config.Website.RomanFootnotes {
return numberToRoman(num)
}
return strconv.Itoa(num)
}
12 changes: 11 additions & 1 deletion ishmael/darkness.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ input = ".org"
# The output file format, defaults to ".html"
output = ".html"
# Relative paths of directories, which will be excluded
exclude = ["excluded"]
exclude = ["do_not_look_here"]

# This is everything regarding website and some tags
[website]
Expand All @@ -33,6 +33,16 @@ styles = ["css/darkness.css"]
# The relative paths of directories to include a tomb
# symbol to the last paragraph to signify its end
tombs = ["blog"]
# Preview is the filename of the picture in the
# same directory to use as a page preview. defaults to preview.png
preview = "preview.jpg"
# Description length dictates on how many characters do we extract
# from the page to show in the web prewies, like OpenGraph and Twitter
description_length = 137
# Normalize headings will shift heading levels if enabled
normalize_headings = false
# Roman Footnotes tells if we have to use roman numerals for footnotes
roman_footnotes = true

# This is the author header section
[author]
Expand Down

0 comments on commit 51d4cda

Please sign in to comment.