with LaTeX \texttt
*/
fun changeCode() {
content = content.replaceTag("", "
", "\\texttt{", "}")
}
/**
* Replace with LaTeX \emph
*/
fun changeItalics() {
content = content.replaceTag("", "", "\\emph{", "}")
}
/**
* Replace with LaTeX footNote url
*/
fun changeLink() {
content = content.replace("{{ site.baseurl }}/", "www.theshell.ch/") // ""
val pattern = Pattern.compile("(.*?)")
val matcher = pattern.matcher(content)
while (matcher.find()) {
val text = matcher.group(3)
val url = matcher.group(1)
content = content.replace(matcher.group(0), LINK.format(url, text))
}
}
/**
* Replace and with LaTeX itemize
*/
fun changeList() {
content = content.replaceTag("", "
", "\\begin{itemize}", "\\end{itemize}")
.replaceTag("", "
", "\\begin{itemize}", "\\end{itemize}")
}
/**
* Replace - with LaTeX itemize item
*/
fun changeListItem() {
content = content.replaceTag("
- ", "
", "\\item ", "")
}
/**
* Replace and {% highlight %} with LaTeX verbatim
*/
fun changeMono() {
content = content.replaceTag("", "
", "\\begin{verbatim}", "\\end{verbatim}")
.replaceTag("{% highlight bash %}", "{% endhighlight %}",
"\\begin{verbatim}", "\\end{verbatim}")
}
/**
* Replace , empty , and with LaTeX plain text
*/
fun changeParagraph() {
content = content.replaceTag("", "
", "", "")
.replaceTag("", "
", "", "")
.replaceTag("
", "
", "\n", "")
.replaceTag("", "", "", "")
if (singlePage) {
content = content.replaceTag("
", "
", "", "")
}
}
/**
* Replace with LaTeX \section
*/
fun changeSection() {
content = content.replaceTag("", "
",
if (singlePage) "\\subsection{" else "\\section{", "}\n")
}
/**
* Replace with LaTeX \subsection
*/
fun changeSubSection() {
content = content.replaceTag("", "
",
if (singlePage) "\\subsubsection{" else "\\subsection{", "}\n")
}
/**
* Replace with LaTeX \subsubsection
*/
fun changeSubSubSection() {
if (singlePage) {
return
}
content = content.replaceTag("", "
", "\\subsubsection{", "}\n")
}
/**
* Replace
with LaTex tabular
*/
fun changeTable() {
val pattern = Pattern.compile("(?:((?:.*?\\r?\\n?)*)
)+")
val matcher = pattern.matcher(content)
while (matcher.find()) {
val document = Jsoup.parse(matcher.group(0), "", Parser.xmlParser())
val table = document.select("table")[0]
val rows = table.select("tr")
val columnsDump = ArrayList()
var colCount = 0
for (row in rows) {
var columns = row.select("td")
if (columns.isEmpty()) {
// Maybe this is an header
columns = row.select("th")
}
if (colCount == 0) {
colCount = columns.size
}
val dump = StringBuilder()
columns.map { it.wholeText() }.forEach { dump.append("$it &") }
// Replace the last & with a LaTeX newLine
columnsDump.add(dump.removeSuffix("&").append("\\\\\n"))
}
// Build the LaTeX table
val latexTable = StringBuilder().run {
append("\\begin{table}[h]\n\\begin{tabular}{${"l".repeat(colCount)}}\n")
columnsDump.forEach { append(" $it") }
append("\\end{tabular}\n\\end{table}")
toString()
}
content = content.replace(matcher.group(0), latexTable)
}
}
/**
* Replace with LaTeX \underline
*/
fun changeUnderline() {
content = content.replaceTag("", "", "\\underline{", "}")
}
/**
* Add an author to the document
*
* @param author the text being inserted
*/
fun addAuthor(author: String) {
insert(if (singlePage) "\\large $author \\normalsize\\\\" else AUTHOR.format(author), true, true)
}
/**
* Add a title to the document
*
* @param title the text being inserted
*/
fun addTitle(title: String) {
insert(if (singlePage) "\\section{$title}\n" else TITLE.format(title), true, true)
}
/**
* Add an header for the html file in singlePage mode
*/
fun addSinglePageInfo(title: String, author: String) {
if (!singlePage) {
return
}
insert(TITLE.format(title), true, true)
insert(AUTHOR.format(author), false, true)
}
/**
* Remove the html comments
*/
fun stripComments() {
content = content.replace(Regex("(?s)"), "")
}
/**
* Insert content into the document
*
* @param string the content being inserted
* @param preLine whether a newLine char should be inserted before the content
* @param afterLine whether a newLine char should be inserted after the content
*/
private fun insert(string: String, preLine: Boolean = false, afterLine: Boolean = false) {
document.apply {
if (preLine) {
append("\n")
}
append(string)
if (afterLine) {
append("\n")
}
}
}
private fun String.replaceTag(oldOpen: String, oldClose: String?,
newOpen: String, newClose: String?): String {
var replaced = replace(oldOpen, newOpen)
if (oldClose != null && newClose != null) {
replaced = replaced.replace(oldClose, newClose)
}
return replaced
}
private fun CharSequence.append(append: String) = "${toString()}$append"
override fun toString() = document.toString()
companion object {
private const val AUTHOR = "\\author{%1\$s}"
private const val TITLE = "\\title{%1\$s}"
private const val LINK = "\\underline{\\href{%1\$s}{%2\$s}}"
private const val HEADER =
"\\documentclass[hidelinks,12pt,a4paper,numbers=enddot]{scrartcl}\n\n" +
"\\usepackage[margin=2cm]{geometry}\n" +
"\\usepackage{hyperref}"
}
}