Skip to content

Commit

Permalink
Add support for \usepackage
Browse files Browse the repository at this point in the history
  • Loading branch information
corollari committed Jun 15, 2019
1 parent 0cf9ab7 commit 094cfb5
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ try{
return
}

input=replaceLiteral(input, "€€")
input=replaceLiteral(input, "€")
input=replaceLiteral(input, "€€", "$$asciimath", "asciimath$$")
input=replaceLiteral(input, "€", "$asciimath", "asciimath$")
//input=replaceLiteral(input, "$$$", "```{=latex}", "```")

let usepackages

[usepackages, input]=removeUsePackage(input)

const pandocFilterPath = path.join(__dirname, "pandoc-filter.js")

let result = child_process.spawnSync("pandoc", ["-t", "latex", "-f", "markdown+lists_without_preceding_blankline+hard_line_breaks", "-s", "--filter", pandocFilterPath], { input: input }).stdout
let result = child_process.spawnSync("pandoc", ["-t", "latex", "-f", "markdown+lists_without_preceding_blankline+hard_line_breaks+raw_tex+raw_attribute", "-s", "--filter", pandocFilterPath], { input: input }).stdout

result=String(result)

Expand All @@ -42,6 +47,8 @@ result=result.replace(/\$asciimath/g, "€")
result=result.replace(/asciimath\$\$/g, "€€")
result=result.replace(/asciimath\$/g, "€")

result=result.replace(/(\\documentclass.*)/g, "$1\n"+usepackages)


let filename = path.basename(inputFile).split(".")
const extension = filename.pop()
Expand All @@ -50,16 +57,15 @@ fs.writeFileSync(filename+".cautex", result)

child_process.spawnSync(typesetter, process.argv.slice(2,-1).concat([filename+".cautex"]), { stdio: 'inherit' })

function replaceLiteral(input, literal){
function replaceLiteral(input, literal, newLiteral1, newLiteral2){
let position=input.indexOf(literal)
let first=true
const delimiter=literal.length==1?"$":"$$"
while(position != -1){
let newLiteral=""
if(first){
newLiteral=delimiter+"asciimath"
newLiteral=newLiteral1
} else {
newLiteral="asciimath"+delimiter
newLiteral=newLiteral2
}
input=input.substring(0, position)+newLiteral+input.substring(position+literal.length, input.length)
first=!first
Expand All @@ -68,6 +74,16 @@ function replaceLiteral(input, literal){
return input
}

function removeUsePackage(input){
const usePkg=/\\usepackage{.*}/g
let usepackages = []
while ((matches = usePkg.exec(input)) !== null) {
usepackages.push(matches[0])
}
input=input.replace(usePkg, "")
return [usepackages.join('\n'), input]
}

function printHelp(){
child_process.exec("groff -man -Tascii "+path.join(__dirname, "man", "caou.1"), (error, stdout, stderr) => console.log(stdout))
}

0 comments on commit 094cfb5

Please sign in to comment.