Skip to content

Commit

Permalink
Add MVP 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
corollari committed Jun 12, 2019
1 parent bb7cdb1 commit bb374e7
Show file tree
Hide file tree
Showing 12 changed files with 1,323 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -39,3 +39,7 @@ Cons:
- Will require all latex code to be properly encapsulated
- Further development will be harder

## Acknowledgements
- pandoc
- The code used to convert asciimath to latex was taken from [here](https://github.com/asciimath/asciimathml/blob/master/asciimath-based/ASCIIMathTeXImg.js). For more information check the License text in `ASCIIMathTeXImg.js`.

30 changes: 30 additions & 0 deletions index.js
@@ -0,0 +1,30 @@
#!/usr/bin/env node

const fs = require('fs')
const path = require('path')
const child_process = require("child_process");
let input = fs.readFileSync(0, 'utf-8')

input=replaceLiteral(input, "€€")
input=replaceLiteral(input, "€")

console.log(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

console.log(String(result))

fs.writeFileSync("input.tex", String(result))

function replaceLiteral(input, literal){
let position=input.indexOf(literal)
let first=true
while(position!=-1){
input=input.substring(0, position)+"\n\`\`\`"+(first?"asciimath"+String(literal.length):"")+"\n"+input.substring(position+2, input.length)
first=!first
position=input.indexOf("€€")
}
return input
}
1,070 changes: 1,070 additions & 0 deletions lib/ASCIIMathTeXImg.js

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions package-lock.json

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

28 changes: 28 additions & 0 deletions package.json
@@ -0,0 +1,28 @@
{
"name": "caoutchouc",
"version": "0.1.0",
"description": "Caoutchouc compiler",
"main": "index.js",
"directories": {
"test": "tests"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/corollari/caoutchouc.git"
},
"keywords": [
"latex"
],
"author": "corollari",
"license": "MIT",
"bugs": {
"url": "https://github.com/corollari/caoutchouc/issues"
},
"homepage": "https://github.com/corollari/caoutchouc#readme",
"dependencies": {
"pandoc-filter-promisified": "^0.1.0"
}
}
33 changes: 33 additions & 0 deletions pandoc-filter.js
@@ -0,0 +1,33 @@
#!/usr/bin/env node

const pandoc = require('pandoc-filter-promisified')
const AsciiMath2Latex = require('./lib/ASCIIMathTeXImg.js')
const { CodeBlock, RawBlock } = pandoc

const HEADER = "asciimath"

pandoc.stdio(action)

async function action(elt, pandocOutputFormat, meta) {
if (elt.t === `CodeBlock`) {
// console.warn(JSON.stringify(elt, null, 4));
const [headers, content] = elt.c

const literalLength = getAsciiMathLiteral(headers)
if(literalLength === 0) return

const literal = literalLength==2?"$$":"$"

let newContent = literal+AsciiMath2Latex(content)+literal

return RawBlock('latex', newContent)
}
}

function getAsciiMathLiteral(headers) {
const [_, classes, keyValuePairs] = headers

if(classes.includes(HEADER+"1")) return 1
if(classes.includes(HEADER+"2")) return 2
return 0
}
3 changes: 3 additions & 0 deletions tests/asciimath.cau
@@ -0,0 +1,3 @@
€€
1/2*alpha
€€
61 changes: 61 additions & 0 deletions tests/asciimath.tex
@@ -0,0 +1,61 @@
\PassOptionsToPackage{unicode=true}{hyperref} % options for packages loaded elsewhere
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provides euro and other symbols
\else % if luatex or xelatex
\usepackage{unicode-math}
\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\usepackage{hyperref}
\hypersetup{
pdfborder={0 0 0},
breaklinks=true}
\urlstyle{same} % don't use monospace font for urls
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

% set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother


\date{}

\begin{document}

$$\frac{{1}}{{2}}\cdot\alpha$$

\end{document}
8 changes: 8 additions & 0 deletions tests/basic.cau
@@ -0,0 +1,8 @@
# Header
This is a basic equation €1+2*sqrt(2)-pi+alpha/(3.2-1)€
Display math:
€€
sum 1
€€
More complex things can be done with latex:
$$x\overset{\Bbb{R}}{+} y=x\overset{\Bbb{Q}}{+} y$$
3 changes: 3 additions & 0 deletions tests/intermediate-asciimath.cau
@@ -0,0 +1,3 @@
```asciimath2
1/2*alpha
```
61 changes: 61 additions & 0 deletions tests/intermediate-asciimath.tex
@@ -0,0 +1,61 @@
\PassOptionsToPackage{unicode=true}{hyperref} % options for packages loaded elsewhere
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provides euro and other symbols
\else % if luatex or xelatex
\usepackage{unicode-math}
\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\usepackage{hyperref}
\hypersetup{
pdfborder={0 0 0},
breaklinks=true}
\urlstyle{same} % don't use monospace font for urls
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi

% set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother


\date{}

\begin{document}

\frac{{1}}{{2}}\cdot\alpha

\end{document}

0 comments on commit bb374e7

Please sign in to comment.