Skip to content
Browse files

first steps toward a new language for Qute

  • Loading branch information...
1 parent 8088981 commit 22116bac6c11d5d0ad400ef38c145c8fb0278cb8 @fbreuer committed Mar 14, 2012
Showing with 111 additions and 3 deletions.
  1. +4 −3 src/inkline.js
  2. +107 −0 src/lang/towards-a-new-language.qute
View
7 src/inkline.js
@@ -218,10 +218,11 @@ function translationError(m,i) {
throw objectThatDelegatesTo(fail, {errorPos: i, errorMsg: "Translation Error"})
}
-function createTransformWrapper(f) {
+function createTransformWrapper(f,tex) {
return function(source,block,outputElt) {
html = f(source)
outputElt.innerHTML = html
+ if(tex) { MathJax.Hub.Queue(["Typeset",MathJax.Hub,outputElt]) }
displayBlock(block)
}
}
@@ -270,7 +271,7 @@ function transformBlock(block) {
try {
trafo = processOMeta(content)
if(typeof trafo == "function") {
- transformers[param] = createTransformWrapper(trafo)
+ transformers[param] = createTransformWrapper(trafo,true)
console.log("New transformer for language " + param + " added to collection of transformers.")
}
} catch(e) {
@@ -289,7 +290,7 @@ function transformBlock(block) {
if(thetransformer == null) {
// if we don't have a custom transformer for the language, we
// use the identity transform
- thetransformer = createTransformWrapper(identityTransform)
+ thetransformer = createTransformWrapper(identityTransform,false)
}
try {
thetransformer(content,block,outputElt)
View
107 src/lang/towards-a-new-language.qute
@@ -0,0 +1,107 @@
+# Towards a new Language
+
+Qute is in need of a new default syntax.
+
+* It should be relatively easy to parse and easy to extend.
+* It should allow text, math, code and environments.
+* It should be convenient to type by hand.
+
+Skribe goes in the right direction. But it is not quite as convenient as I would like.
+
+## Examples
+
+Let's start with an example. The following is a snippet from one of my math papers.
+
+@ignore
+Let $p(k)$ be a polynomial in $k$ of degree at most $d$. Then there exist coefficients $h^*_i$ and $f^*_i$ for $i=0,\ldots,d$ such that
+\begin{eqnarray}
+p(k) = \sum_{i=0}^d h^*_i {k+d-i \choose d} = \sum_{i=0}^d f^*_i {k-1 \choose i}.
+\end{eqnarray}
+The coefficients $h^*_i$ and $f^*_i$ depend both on $p$ and on $d$, so we will sometimes write $h^*_i(p,d)$ and $f^*_i(p,d)$ to make this dependency explicit. The vectors $(h^*_0,\ldots,h^*_d)$ and $(f^*_0,\ldots,f^*_d)$ are called the \emph{$h^*$- and $f^*$-vectors} of $p$ and their entries are the \emph{$h^*$- and $f^*$-coefficients} of $p$, respectively. Note that the $h^*$-vector also goes by the name of Ehrhart $\delta$-vector. \cite{Stapledon2009} Whenever we refer to the $h^*$- or $f^*$-vector of an integral polytope or polytopal complex $P$, we mean the $h^*$- or $f^*$-vector of its Ehrhart polynomial $L_P$. For more details on these vectors and, most importantly, the motivation for defining them we refer the reader to Section~\ref{sec:prelim-f-and-h}.
+
+How would I want this to look in source?
+
+@ignore Let $p(k)$ be a polynomial in $k$ of degree at most $d$. Then there exist coefficients $h^*_i$ and $f^*_i$ for $i=0,\ldots,d$ such that
+,(eqnarray
+ $p(k) = \sum_{i=0}^d h^*_i {k+d-i \choose d} = \sum_{i=0}^d f^*_i {k-1 \choose i}.$
+)
+The coefficients $h^*_i$ and $f^*_i$ depend both on $p$ and on $d$, so we will sometimes write $h^*_i(p,d)$ and $f^*_i(p,d)$ to make this dependency explicit. The vectors $(h^*_0,\ldots,h^*_d)$ and $(f^*_0,\ldots,f^*_d)$ are called the \emph [$h^*$- and $f^*$-vectors]\ of $p$ and their entries are the ,(emph [$h^*$- and $f^*$-coefficients]) of $p$, respectively. Note that the $h^*$-vector also goes by the name of Ehrhart $\delta$-vector. ,(cite "Stapledon2009") Whenever we refer to the $h^*$- or $f^*$-vector of an integral polytope or polytopal complex $P$, we mean the $h^*$- or $f^*$-vector of its Ehrhart polynomial $L_P$. For more details on these vectors and, most importantly, the motivation for defining them we refer the reader to Section,(nbsp),(ref "sec:prelim-f-and-h").
+
+We also want to have custom commands in Math mode, without relying on MathJax macro expansion facilities. But maybe I should leave this for later.
+
+## Toggles between text, math and code
+
+Here are three simple parse rules.
+
+* Math is delimited by "\$". The string between the delimiting "\$" is parsed *only* according to math rules, i.e., for the moment, this is not touched at all and left entirely to MathJax. All literal "\$" in math mode have to be escaped. As this is the same convention in TeX, we even leave the escape characters alone.
+* Code is delimited by ",(" and ")" or "@(" and ")". The string in between these delimiters parsed according to scheme rules. This might turn out to be a problem for some programming languages, but we'll have to see.
+* Text is delimited by "[" and "]".
+
+In fact, "code" is the wrong word. Code is not "code" at all. It is just that we are defining a parse tree *explicitly* here.
+
+## A First Implementation
+
+@ometa:qute
+ometa Qute <: Parser {
+ delimiter = '[' | ']' | '{' | '}' | '$',
+ text = '[' textbody:t ']' -> t,
+ textbody = (str | math | list)*:els -> [#text].concat(els),
+ list = '{' ((text | math | list | symbol):a spaces -> a)*:as '}' -> [#list].concat(as),
+ math = '$' tex:t '$' -> [#math].concat(t),
+ escapeChar = '\\' char:c -> c,
+ str = (escapeChar:c -> c | ~delimiter char)+:cs -> [#str].concat(cs.join("")),
+ tex = (escapeChar:c -> ["\\",c].join("") | ~'$' char)*:cs -> [#tex].concat(cs.join("")),
+ symbol = (~(space|delimiter) char)+:cs -> [#sym, cs.join("")]
+}
+Qute.initialize = function() { this.vars = {}; }
+function(source) {return Qute.matchAll(source,'textbody',undefined, parseError)}
+
+
+
+Here are some tests.
+
+@qute foo bar baz
+
+@qute Here comes a longer example, including {bold}.
+
+@qute Here comes a longer example with escape \$, including {bold [text]}.
+
+@qute Here comes a longer example with escape \$, including {bold [bold text]}, lists with more complicated nesting such as {itemize [First item,] [Second item, and] [Third item.] } and of course TeX such as $x^2+y^2$. By the way, it is important that the backslash character \\ does not need to be escaped in math mode, because of $\begin{foo}blah\end{bar}$. And it is not!
+
+## An Implementation with Translator
+
+@ometa:qute2
+ometa Qute <: Parser {
+ delimiter = '[' | ']' | '{' | '}' | '$',
+ text = '[' textbody:t ']' -> t,
+ textbody = (str | math | list)*:els -> [#text].concat(els),
+ list = '{' ((text | math | list | symbol):a spaces -> a)*:as '}' -> [#list].concat(as),
+ math = '$' tex:t '$' -> [#math].concat(t),
+ escapeChar = '\\' char:c -> c,
+ str = (escapeChar:c -> c | ~delimiter char)+:cs -> [#str].concat(cs.join("")),
+ tex = (escapeChar:c -> ["\\",c].join("") | ~'$' char)*:cs -> cs.join(""),
+ symbol = (~(space|delimiter) char)+:cs -> [#symbol, cs.join("")]
+}
+Qute.initialize = function() { this.vars = {}; }
+ometa QuteTranslator {
+ trans = [:t apply(t):ans] -> ans,
+ text trans*:xs -> xs.join(""),
+ list trans*:xs -> ("<span style='border: 1px solid white; padding: 1px 5px 1px 5px'>" + xs.join("") + "</span>"),
+ str :s -> ("<span style='color:white;font-family: MgOpen Cosmetica; padding: 1px 5px 1px 5px'>" + s + "</span>"),
+ math :s -> ("<script type='math/tex'>" + s + "</script>"),
+ symbol :s -> ("<span style='color:yellow;font-family: monospace; font-size:80%;'>" + s + "</span>")
+}
+QuteTranslator.initialize = function() {this.vars = {};}
+function(source) {
+ tree = Qute.matchAll(source,'textbody',undefined, parseError);
+ return QuteTranslator.match(tree,'trans',undefined,translationError);
+}
+
+
+
+### Tests
+
+@qute2 Here comes a longer example with escape \$, including {bold [bold text]}, lists with more complicated nesting such as {itemize [First item,] [Second item, and] [Third item.] } and of course TeX such as $x^2+y^2$. By the way, it is important that the backslash character \\ does not need to be escaped in math mode, because of $a=\sqrt{2}$. And it is not!
+
+
+

0 comments on commit 22116ba

Please sign in to comment.
Something went wrong with that request. Please try again.