diff options
Diffstat (limited to 'cmd/gobtex')
| -rwxr-xr-x | cmd/gobtex/gobtex | bin | 0 -> 4730480 bytes | |||
| -rw-r--r-- | cmd/gobtex/main.go | 56 | ||||
| -rw-r--r-- | cmd/gobtex/main.js | 14 | ||||
| -rw-r--r-- | cmd/gobtex/style.css | 62 | ||||
| -rw-r--r-- | cmd/gobtex/test.tmpl | 175 |
5 files changed, 282 insertions, 25 deletions
diff --git a/cmd/gobtex/gobtex b/cmd/gobtex/gobtex Binary files differnew file mode 100755 index 0000000..c4bd2ca --- /dev/null +++ b/cmd/gobtex/gobtex diff --git a/cmd/gobtex/main.go b/cmd/gobtex/main.go index 4bbd5b3..30d9628 100644 --- a/cmd/gobtex/main.go +++ b/cmd/gobtex/main.go @@ -2,8 +2,8 @@ package main import ( "bytes" - "encoding/json" "fmt" + "html/template" "log" "os" "strings" @@ -11,6 +11,18 @@ import ( "github.com/Thibauth/bibtex" ) +func printfne(format string, a ...interface{}) string { + if fmt.Sprint(a...) == "" { + return "" + } else { + return fmt.Sprintf(format, a...) + } +} + +func dashify(s string) string { + return strings.Replace(s, "-", "–", -1) +} + func main() { fname := os.Args[1] f, err := os.Open(fname) @@ -18,31 +30,25 @@ func main() { log.Fatal(err) } db, err := bibtex.Parse(f, false) - res, _ := json.MarshalIndent(db, "", " ") - fmt.Println(string(res)) -} - -func MarshalEntry(e bibtex.Entry) string { - var buf bytes.Buffer - temp := make([]string, len(e.FNames)) - open, close := "{", "}" - if strings.Contains(e.Key, "}") { - open, close = "(", ")" - } - buf.WriteString("@" + e.Type + open + e.Key + ",") - if len(e.FNames) == 0 { - buf.WriteString(close) - return buf.String() - } else { - buf.WriteString("\n\t") + db.Flatten() + db.Resolve() + db.SplitNames() + db.UnTex() + tmpl := template.New("test.tmpl").Funcs(template.FuncMap{ + "untex": bibtex.UnTex, + "add": func(a, b int) int { return a + b }, + "ne": printfne, + "dashify": dashify, + "contains": strings.ContainsAny, + }) + tmpl, err = tmpl.ParseFiles("test.tmpl") + if err != nil { + log.Fatal(err) } - - for i, field := range e.FNames { - temp[i] = field + " = " + e.Fields[field].Marshal() + err = tmpl.Execute(os.Stdout, db) + if err != nil { + log.Fatal(err) } - buf.WriteString(strings.Join(temp, ",\n\t")) - buf.WriteString("\n" + close) - return buf.String() } func Marshal(db *bibtex.Database) string { @@ -57,7 +63,7 @@ func Marshal(db *bibtex.Database) string { } res := make([]string, len(db.EKeys)) for i, key := range db.EKeys { - res[i] = MarshalEntry(db.Entries[key]) + res[i] = db.Entries[key].Marshal() } buf.WriteString(strings.Join(res, "\n\n")) return buf.String() diff --git a/cmd/gobtex/main.js b/cmd/gobtex/main.js new file mode 100644 index 0000000..9b1c533 --- /dev/null +++ b/cmd/gobtex/main.js @@ -0,0 +1,14 @@ +document.addEventListener("DOMContentLoaded", function(event) { + main(); +}); + +function main() { + let links = document.getElementsByClassName('biblink'); + for (let i = 0; i < links.length; i++) { + links[i].addEventListener("click", function(event) { + event.preventDefault(); + let pre = this.parentNode.getElementsByTagName('pre')[0]; + pre.classList.toggle('visible'); + }); + } +} diff --git a/cmd/gobtex/style.css b/cmd/gobtex/style.css new file mode 100644 index 0000000..7a686f5 --- /dev/null +++ b/cmd/gobtex/style.css @@ -0,0 +1,62 @@ +.title { + font-weight: bold +} + +li { + margin: 1em 0; + padding: 0 +} + +.journal { + font-style: italic +} + +pre { + background-color: rgb(247, 247, 247); + tab-size: 4; + -moz-tab-size: 4; + white-space: pre-wrap; + display: none; + padding: 1em; +} + +pre.visible { + display: block; +} + +body { + width: 1000px; + margin: auto; + font-family: sans-serif +} + +ul { + list-style: none; + margin: 0; + padding: 0 +} + +.entrylink { + color: #c60f0f; + display: none; + padding: 0 4px; +} + +.entrylink:hover { + color: white; + background-color: #c60f0f; + text-decoration: none; +} + +li:hover .entrylink { + display: inline; +} + +a { + text-decoration: none; + color: rgb(64, 120, 192); +} + +a:hover { + text-decoration: underline; +} diff --git a/cmd/gobtex/test.tmpl b/cmd/gobtex/test.tmpl new file mode 100644 index 0000000..5a5bc83 --- /dev/null +++ b/cmd/gobtex/test.tmpl @@ -0,0 +1,175 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <title>title</title> + <link rel="stylesheet" href="style.css"> + </head> + <body> + <ul> + {{- range $key := .EKeys -}} + {{- if not (index $.CrossRefs $key) -}} + {{- with index $.Entries $key }} + <li id="{{ .Key }}"> + <span class="title">{{.Fields.title}}</span> {{template "links" .Fields}}<br/> + by {{template "authors" .Fields.author}}<br/> + {{if eq .Type "phdthesis" -}} + {{template "phdthesis" .Fields}}. + {{- else if eq .Type "mastersthesis" -}} + {{template "mastersthesis" .Fields}}. + {{- else if eq .Type "article" -}} + {{template "article" .Fields}}. + {{- else if eq .Type "incollection" -}} + {{template "incollection" .Fields}}. + {{- else if eq .Type "misc" -}} + {{template "misc" .Fields}}. + {{- else if eq .Type "book" -}} + {{template "book" .Fields}}. + {{- else if eq .Type "inproceedings" -}} + {{template "inproceedings" .Fields}}. + {{- else if eq .Type "techreport" -}} + {{template "techreport" .Fields}}. + {{- end}} <a class="entrylink" href="#{{ .Key}}">¶</a> + <pre>{{ .Marshal}}</pre> + </li> + {{- end -}} + {{- end -}} + {{- end}} + </ul> + </body> + <script src="main.js"></script> +</html> + +{{- define "article" -}} +In <span class="journal">{{.journal}}</span>{{template "vnp" .}}{{template "date" .}} +{{- end -}} + +{{- define "book" -}} +{{template "Volume" .}}{{template "nseries" .}}{{.publisher | ne ". %s"}}{{.address | ne ", %s" -}} +{{.edition | ne ", %s"}}{{template "date" .}} +{{- end -}} + +{{- define "incollection" -}} +{{template "edtitle" .}}{{template "volume" .}}{{template "nseries" .}}{{template "chapterpages" . -}} +{{.publisher | ne ". %s"}}{{.address | ne ", %s"}}{{template "date" .}} +{{- end -}} + +{{- define "inproceedings" -}} +{{template "edtitle" .}}{{template "volume" .}}{{ template "nseries" .}}{{template "page" .}} +{{- if not .address -}} + {{- if or .organization .publisher -}} + . {{.organization | ne "%s"}}{{.publisher | ne ", %s"}} + {{- end -}} + {{template "date" .}} +{{- else -}} +{{.address | ne ", %s"}}{{template "date" .}}{{.organization | ne ". %s"}}{{.publisher | ne ", %s"}} +{{- end -}} +{{- end -}} + +{{- define "mastersthesis" -}} +{{or .type "Master's thesis"}}{{.school | ne ", %s"}}{{.address | ne ", %s"}}{{template "date" .}} +{{- end -}} + +{{- define "misc" -}} +{{if .title}}{{.title}}{{.howpublished | ne ", %s"}}{{else}}{{.howpublished}}{{end}}{{template "date" .}} +{{- end -}} + +{{- define "phdthesis" -}} +{{or .type "PhD thesis"}}{{.school | ne ", %s"}}{{.address | ne ", %s"}}{{template "date" .}} +{{- end -}} + +{{- define "techreport" -}} +{{or .type "Technical Report"}}{{.institution | ne ", %s"}}{{template "date" .}} +{{- end -}} + +{{- define "edtitle" -}} +{{- if .booktitle -}} +{{- if .editor -}} +In {{template "authors" .editor}}, {{if gt (len .editor) 1}}editors{{else}}editor{{end}}, <span class="journal">{{.booktitle}}</span> +{{- else -}} +In <span class="journal">{{.booktitle}}</span> +{{- end -}} +{{- end -}} +{{- end -}} + +{{- define "volume" -}} +{{- if .volume -}} +{{.volume | ne ", volume %s"}} +{{- if .series}} of <span class="series">{{.series}}</span>{{- end -}} +{{- end -}} +{{- end -}} + +{{- define "Volume" -}} +{{- if .volume -}} +{{.volume | ne "Volume %s"}} +{{- if .series}} of <span class="series">{{.series}}</span>{{- end -}} +{{- end -}} +{{- end -}} + +{{- define "chapterpages" -}} +{{- if .chapter -}} +{{or .type "chapter" | ne ", %s"}} {{.chapter}}{{template "page" .}} +{{- end -}} +{{- end -}} + +{{- define "nseries" -}} +{{- if not .volume -}} + {{- if not .number -}} + {{.series | ne ", %s"}} + {{- else -}} + , number {{.number}} in {{.series}} + {{- end -}} +{{- end -}} +{{- end -}} + +{{- define "vnp" -}} +{{- if .volume -}} + {{.volume | ne ", %s"}}{{.number | ne "(%s)"}} + {{- .pages.String | print | dashify | ne ":%s"}} +{{- else -}} + {{template "page" .}} +{{- end -}} +{{- end -}} + +{{- define "page" -}} +{{if .pages }}{{if contains .pages.String "-–,+"}}, pages {{else}}, page {{end}}{{.pages.String | dashify | printf "%s"}}{{end}} +{{- end -}} + +{{- define "date" -}} +{{if .year }}, <span class="date">{{.month | ne "%s "}}{{.year}}</span>{{end}} +{{- end -}} + +{{- define "links" -}} +{{- if .www_pdf_url -}} + [<a href="{{ .www_pdf_url}}">pdf</a>] +{{- end -}} +{{- if .www_ps_url -}} + [<a href="{{.www_ps_url}}">ps</a>] +{{- end -}} +{{- if .www_html_url -}} + [<a href="{{ .www_html_url}}">html</a>] +{{- end -}} +[<a class="biblink" href="#">bib</a>] +{{- end -}} + +{{- define "authors" -}} +{{$len := len .}} +{{- if eq $len 1 -}} + {{template "author" index . 0}} +{{- else if eq $len 2 -}} + {{template "author" index . 0}}{{" and "}} {{template "author" index . 1}} +{{- else -}} + {{range $index, $name := . -}} + {{template "author" .}} + {{- if eq $index (add $len -2) -}} + {{", and "}} + {{- else if lt $index (add $len -2) -}} + {{", "}} + {{- end -}} + {{- end -}} +{{- end -}} +{{- end -}} + +{{- define "author" -}} +<span class="author">{{.Format "{f.~}{vv~}{ll}{, jj}" | untex}}</span> +{{- end -}} |
