Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Use blackfriday LaTeX export for all values inserted in LaTeX.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Kittelberger committed Feb 20, 2017
1 parent 340b6f2 commit 9a1ae64
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 36 deletions.
4 changes: 2 additions & 2 deletions export/latex/localization/de-de.all.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"translation": "Berufsschule"
},
{
"id": "proof_of_education",
"translation": "Ausbildungsnachweis Nr. {{.Count}}"
"id": "proof_of_education_prefix",
"translation": "Ausbildungsnachweis Nr."
},
{
"id": "trainee",
Expand Down
4 changes: 2 additions & 2 deletions export/latex/localization/en-us.all.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"translation": "Professional school"
},
{
"id": "proof_of_education",
"translation": "Proof of Education No. {{.Count}}"
"id": "proof_of_education_prefix",
"translation": "Proof of Education No."
},
{
"id": "trainee",
Expand Down
8 changes: 4 additions & 4 deletions export/latex/localization_assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion export/latex/stringutil/converter.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package stringutil

type converter interface {
type Converter interface {
Process(text string) string
}
2 changes: 1 addition & 1 deletion export/latex/stringutil/converter_simpleregex.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type simpleRegexConverter struct {
replacement string
}

func newSimpleRegexConverter(regexStr string, replacementStr string) *simpleRegexConverter {
func NewSimpleRegexConverter(regexStr string, replacementStr string) *simpleRegexConverter {
return &simpleRegexConverter{
regex: regexp.MustCompile(regexStr),
replacement: replacementStr,
Expand Down
2 changes: 1 addition & 1 deletion export/latex/stringutil/converter_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type stringConverter struct {
replacement string
}

func newStringConverter(old string, replacementStr string) *stringConverter {
func NewStringConverter(old string, replacementStr string) *stringConverter {
return &stringConverter{
old: old,
replacement: replacementStr,
Expand Down
28 changes: 11 additions & 17 deletions export/latex/stringutil/escape.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
package stringutil

var replacements = []converter{
newStringConverter("{", "\\{"),
newStringConverter("}", "\\}"),
newStringConverter("\\", "\\textbackslash{}"),
// BackslashEscape modifies a string so any occurrences of given substrings
// are prefixed with a backslash.
func BackslashEscape(s string, substrings ...string) string {
replacements := []Converter{}

newStringConverter("&", "\\&"),
newStringConverter("%", "\\%"),
newStringConverter("$", "\\$"),
newStringConverter("#", "\\#"),
newStringConverter("_", "\\_"),
newStringConverter("~", "\\textasciitilde{}"),
newStringConverter("^", "\\textasciicircum{}"),
newStringConverter("ß", "\\ss{}"),
for _, substring := range substrings {
replacements = append(replacements, NewStringConverter(substring, "\\"+substring))
}

newSimpleRegexConverter(`"([^"]+)"`, `\enquote{$1}`),
newSimpleRegexConverter("`([^`]+)`", "\\verb`$1`"),
return CustomEscape(s, replacements...)
}

// TexEscape modifies a string so it can be safely places in a LaTeX file
// without causing any errors due to special characters.
func TexEscape(s string) string {
// CustomEscape modifies a string according to the given conversion rules in
// the exact given order.
func CustomEscape(s string, replacements ...Converter) string {
for _, replacer := range replacements {
s = replacer.Process(s)
}
Expand Down
51 changes: 51 additions & 0 deletions export/latex/stringutil/latexize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package stringutil

import (
"strings"

"git.dekart811.net/icedream/workreportmgr/internal/util"
"github.com/russross/blackfriday"
)

const (
enabledExtensions = 0 |
blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
blackfriday.EXTENSION_TABLES |
blackfriday.EXTENSION_FENCED_CODE |
blackfriday.EXTENSION_AUTOLINK |
blackfriday.EXTENSION_STRIKETHROUGH |
blackfriday.EXTENSION_SPACE_HEADERS |
blackfriday.EXTENSION_HEADER_IDS |
blackfriday.EXTENSION_BACKSLASH_LINE_BREAK |
blackfriday.EXTENSION_DEFINITION_LISTS
)

var additionalTexReplacements = []Converter{
// NewStringConverter("{", "\\{"),
// NewStringConverter("}", "\\}"),
// NewStringConverter("\\", "\\textbackslash{}"),
//
// NewStringConverter("&", "\\&"),
// NewStringConverter("%", "\\%"),
// NewStringConverter("$", "\\$"),
// NewStringConverter("#", "\\#"),
// NewStringConverter("_", "\\_"),
// NewStringConverter("~", "\\textasciitilde{}"),
// NewStringConverter("^", "\\textasciicircum{}"),
// NewStringConverter("ß", "\\ss{}"),

NewSimpleRegexConverter(`"([^"]+)"`, `\enquote{$1}`),
// NewSimpleRegexConverter("`([^`]+)`", "\\verb`$1`"),
}

/*
Latexize takes an input text as parsed from the value of any field in a project
file and turns it into LaTeX code.
*/
func Latexize(input string) string {
renderer := util.DocumentlessRenderer{Renderer: blackfriday.LatexRenderer(0)}
renderedString := string(blackfriday.Markdown([]byte(input), renderer, enabledExtensions))
renderedString = CustomEscape(renderedString, additionalTexReplacements...)
renderedString = strings.TrimSpace(renderedString)
return renderedString
}
16 changes: 8 additions & 8 deletions export/latex/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ var exportTemplate = template.Must(template.
"endofweek": func(date project.Date) project.Date {
return project.Date{Time: now.New(date.Time).EndOfWeek()}
},
"escape": stringutil.TexEscape,
"add": func(a, b int) int {
return a + b
},
"latexize": stringutil.Latexize,
}).
Delims("<", ">").
Parse(`% !TeX
Expand Down Expand Up @@ -89,8 +89,8 @@ var exportTemplate = template.Must(template.
\setcounter{section}{\wrNumber}
\setcounter{subsection}{0}
\section*{<T "proof_of_education" "\\wrNumber">}
\addcontentsline{toc}{section}{<T "proof_of_education" "\\wrNumber">}
\section*{<T "proof_of_education_prefix"> \wrNumber}
\addcontentsline{toc}{section}{<T "proof_of_education_prefix"> \wrNumber}
}{
}
Expand Down Expand Up @@ -119,25 +119,25 @@ var exportTemplate = template.Must(template.
<with $week.WorkActivities>
\begin{itemize}
<range .>
\item <. | escape>
\item <. | latexize>
<end>
\end{itemize}
<end>
\weeklyreportsection{<T "operational_instruction">}
<$week.WorkActivityDetails | escape>
<$week.WorkActivityDetails | latexize>
\weeklyreportsection{<T "professional_school">}
<if eq (len $week.Periods) 0>
<T "no_school_periods_this_week" | escape>
<T "no_school_periods_this_week">
<else>
\begin{itemize}
<range $week.Periods>
\item{
<- .Subject | escape ->
<- .Subject | latexize ->
<- with .Topics ->:
<range $index, $topic := . ->
<- if ne $index 0 ->, <end -><- $topic | escape ->
<- if ne $index 0 ->, <end -><- $topic | latexize ->
<- end ->
<- end ->
}
Expand Down
29 changes: 29 additions & 0 deletions internal/util/documentless_renderer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package util

import (
"bytes"

"github.com/russross/blackfriday"
)

/*
DocumentlessRenderer wraps an actual renderer and suppresses full-document
output.
*/
type DocumentlessRenderer struct {
blackfriday.Renderer
}

/*
DocumentHeader does nothing as it is a bogus implementation to avoid full-
document output.
*/
func (r DocumentlessRenderer) DocumentHeader(out *bytes.Buffer) {
}

/*
DocumentFooter does nothing as it is a bogus implementation to avoid full-
document output.
*/
func (r DocumentlessRenderer) DocumentFooter(out *bytes.Buffer) {
}

0 comments on commit 9a1ae64

Please sign in to comment.