Add rules for glossary, make CSS nicer

This commit is contained in:
Hamcha 2019-06-14 11:37:08 +02:00
parent 4fd6f2d35b
commit 58ddb252fa
Signed by: hamcha
GPG key ID: A40413D21021EAEE
5 changed files with 114 additions and 48 deletions

View file

@ -1,12 +1,19 @@
# htmlroc # htmlroc
Converts MLP:CCG rule files from text (thanks Hithroc!) to HTML! Converts MLP:CCG rule files from text to HTML!
## Installation ## Installation
### Requirements
- Go 1.12+
- The text rules from Hithroc's site:
https://horse.cards/rules/latest.txt
### Running htmlroc
1. Clone project locally and `cd` to it 1. Clone project locally and `cd` to it
2. Download `rules.txt` from Hithroc's site and place it inside the project's folder: 2. Place the text rules inside the project's folder as `rules.txt`
https://horse.cards/rules/latest.txt
3. Run this command: 3. Run this command:
``` ```
go run . go run .
@ -17,4 +24,4 @@ Alternately, you can compile with `go install` and use the command line flags to
## Thanks to ## Thanks to
- [Hithroc](https://hithroc.org/) for maitaining text versions of the rules - [Hithroc](https://hithroc.org/) for maitaining text versions of the rules
- [CommentaryIsMagic](http://www.commentaryismagic.com/) for maitaining the rules - [CommentaryIsMagic](http://www.commentaryismagic.com/) for maitaining the official rules

31
main.go
View file

@ -31,14 +31,11 @@ type TemplateRule struct {
Depth int Depth int
} }
var funmap = template.FuncMap{
"toCredits": tplToCredits,
"htmlify": tplHtmlify,
}
// HTMLrocCredits is the credit string to append to the other credits // HTMLrocCredits is the credit string to append to the other credits
const HTMLrocCredits = "Converted to HTML using <a href=\"https://git.fromouter.space/mcg/htmlroc\">htmlroc</a>" const HTMLrocCredits = "Converted to HTML using <a href=\"https://git.fromouter.space/mcg/htmlroc\">htmlroc</a>"
var tpl *template.Template
func main() { func main() {
// Command line flags // Command line flags
txtfile := flag.String("in", "rules.txt", "Path to rules.txt file") txtfile := flag.String("in", "rules.txt", "Path to rules.txt file")
@ -47,7 +44,8 @@ func main() {
flag.Parse() flag.Parse()
// Read template file // Read template file
tpl, err := template.New(*tplfile).Funcs(funmap).ParseFiles(*tplfile) var err error
tpl, err = template.New(*tplfile).Funcs(funmap).ParseFiles(*tplfile)
checkErr(err, "Could not load template file \"%s\"", *tplfile) checkErr(err, "Could not load template file \"%s\"", *tplfile)
tpldata := TemplateData{} tpldata := TemplateData{}
@ -116,7 +114,7 @@ func main() {
// Create new section // Create new section
currentSection = &TemplateSection{ currentSection = &TemplateSection{
ID: parts[0], ID: parts[0],
Title: parts[1], Title: strings.TrimSpace(parts[1]),
Rules: []TemplateRule{}, Rules: []TemplateRule{},
Extra: false, Extra: false,
} }
@ -166,21 +164,4 @@ func checkErr(err error, fmtstr string, args ...interface{}) {
fmt.Fprintln(os.Stderr, err.Error()) fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
} }
} }
func tplToCredits(str string) template.HTML {
// Make first line an heading
idx := strings.IndexRune(str, '\n')
if idx > 0 {
str = "<h1>" + str[:idx] + "</h1><p>" + str[idx+1:]
}
// Wrap other lines in <p> tags
str = strings.ReplaceAll(str, "\n", "</p><p>") + "</p>"
return template.HTML(str)
}
func tplHtmlify(str string) template.HTML {
// Replace newlines with <br />
str = strings.ReplaceAll(str, "\n", "<br />")
return template.HTML(str)
}

View file

@ -1,46 +1,56 @@
@import url('https://fonts.googleapis.com/css?family=Roboto:300,400&display=swap'); @import url("https://fonts.googleapis.com/css?family=Roboto:300,400,700&display=swap");
body { body {
font-family: 'Roboto', sans-serif; font-family: "Roboto", sans-serif;
font-weight: 300; font-weight: 300;
display: flex; display: flex;
justify-content: center; justify-content: center;
font-size: 11pt; font-size: 11pt;
line-height: 150%;
} }
main { main {
flex: 1; flex: 1;
max-width: 60em; max-width: 60em;
} }
h1, h1,
h2 { h2 {
color: #4b5b69; color: #4b5b69;
font-weight: 400; font-weight: 400;
} }
p { p {
margin: 0; margin: 0;
padding: 2pt 0; padding: 2pt 0;
} }
ul { ul {
padding-left: 10pt; padding-left: 10pt;
} }
li { li {
margin: 10pt 0; margin: 10pt 0;
list-style-type: none; list-style-type: none;
} }
.depth-2 { .depth-2 {
padding-left: 20pt; padding-left: 20pt;
} }
.depth-4 { .depth-4 {
padding-left: 40pt; padding-left: 40pt;
} }
.depth-6 { .depth-6 {
padding-left: 60pt; padding-left: 60pt;
} }
.glossary dt {
font-weight: bold;
}
.glossary dd {
margin-inline-start: 10pt;
margin-bottom: 1rem;
}

59
template.go Normal file
View file

@ -0,0 +1,59 @@
package main
import (
"html/template"
"strings"
)
var funmap = template.FuncMap{
"toCredits": tplToCredits,
"htmlify": tplHtmlify,
"toGlossary": tplToGlossary,
}
func tplToCredits(str string) template.HTML {
// Make first line an heading
idx := strings.IndexRune(str, '\n')
if idx > 0 {
str = "<h1>" + str[:idx] + "</h1><p>" + str[idx+1:]
}
// Wrap other lines in <p> tags
str = strings.ReplaceAll(str, "\n", "</p><p>") + "</p>"
return template.HTML(str)
}
func tplHtmlify(str string) template.HTML {
// Replace newlines with <br />
str = strings.ReplaceAll(str, "\n", "<br />")
return template.HTML(str)
}
type glossaryEntry struct {
Keyword string
Definition string
}
func tplToGlossary(str string) (glob []glossaryEntry) {
lines := strings.Split(str, "\n")
for _, line := range lines {
// Skip empty lines
line = strings.TrimSpace(line)
if len(line) < 1 {
continue
}
// Find delimiter
delim := strings.IndexRune(line, ':')
if delim < 0 {
// No delim? Add to last entry
glob[len(glob)-1].Definition += line
continue
}
glob = append(glob, glossaryEntry{
Keyword: line[:delim],
Definition: strings.TrimSpace(line[delim+1:]),
})
}
return
}

View file

@ -15,7 +15,16 @@
{{ range .Sections }} {{ range .Sections }}
<h2 id="{{ .ID }}">{{ .ID }}. {{ .Title }}</h2> <h2 id="{{ .ID }}">{{ .ID }}. {{ .Title }}</h2>
{{ if .Extra }} {{ if .Extra }}
{{ if (eq .Title "Glossary")}}
<dl class="glossary">
{{ range (toGlossary .Text) }}
<dt>{{ .Keyword }}</dt>
<dd>{{ .Definition }}</dd>
{{ end }}
</dl>
{{ else }}
{{ .Text | htmlify }} {{ .Text | htmlify }}
{{ end }}
{{ else }} {{ else }}
<ul> <ul>
{{ range .Rules }} {{ range .Rules }}