Skip to content

Commit f053e46

Browse files
Jucovschi Constantinmarijnh
authored andcommitted
adding sTeX (semantic LaTeX) mode
1 parent ac0877e commit f053e46

File tree

4 files changed

+284
-0
lines changed

4 files changed

+284
-0
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ <h3>Available modes:</h3>
8383
<li><a href="mode/php/">PHP</a></li>
8484
<li><a href="mode/diff/">Diff</a></li>
8585
<li><a href="mode/haskell/">Haskell</a></li>
86+
<li><a href="mode/stex/">sTeX</a></li>
8687
</ul>
8788

8889
<h3>Usage demos:</h3>

mode/stex/index.html

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>CodeMirror 2: sTeX mode</title>
5+
<link rel="stylesheet" href="../../lib/codemirror.css">
6+
<script src="../../lib/codemirror.js"></script>
7+
<script src="stex.js"></script>
8+
<link rel="stylesheet" href="stex.css">
9+
<style>.CodeMirror {background: #f8f8f8;}</style>
10+
<link rel="stylesheet" href="../../docs.css">
11+
</head>
12+
<body>
13+
<h1>CodeMirror 2: sTeX mode</h1>
14+
<form><textarea id="code" name="code">
15+
\begin{module}[id=bbt-size]
16+
\importmodule[balanced-binary-trees]{balanced-binary-trees}
17+
\importmodule[\KWARCslides{dmath/en/cardinality}]{cardinality}
18+
19+
\begin{frame}
20+
\frametitle{Size Lemma for Balanced Trees}
21+
\begin{itemize}
22+
\item
23+
\begin{assertion}[id=size-lemma,type=lemma]
24+
Let $G=\tup{V,E}$ be a \termref[cd=binary-trees]{balanced binary tree}
25+
of \termref[cd=graph-depth,name=vertex-depth]{depth}$n>i$, then the set
26+
$\defeq{\livar{V}i}{\setst{\inset{v}{V}}{\gdepth{v} = i}}$ of
27+
\termref[cd=graphs-intro,name=node]{nodes} at
28+
\termref[cd=graph-depth,name=vertex-depth]{depth} $i$ has
29+
\termref[cd=cardinality,name=cardinality]{cardinality} $\power2i$.
30+
\end{assertion}
31+
\item
32+
\begin{sproof}[id=size-lemma-pf,proofend=,for=size-lemma]{via induction over the depth $i$.}
33+
\begin{spfcases}{We have to consider two cases}
34+
\begin{spfcase}{$i=0$}
35+
\begin{spfstep}[display=flow]
36+
then $\livar{V}i=\set{\livar{v}r}$, where $\livar{v}r$ is the root, so
37+
$\eq{\card{\livar{V}0},\card{\set{\livar{v}r}},1,\power20}$.
38+
\end{spfstep}
39+
\end{spfcase}
40+
\begin{spfcase}{$i>0$}
41+
\begin{spfstep}[display=flow]
42+
then $\livar{V}{i-1}$ contains $\power2{i-1}$ vertexes
43+
\begin{justification}[method=byIH](IH)\end{justification}
44+
\end{spfstep}
45+
\begin{spfstep}
46+
By the \begin{justification}[method=byDef]definition of a binary
47+
tree\end{justification}, each $\inset{v}{\livar{V}{i-1}}$ is a leaf or has
48+
two children that are at depth $i$.
49+
\end{spfstep}
50+
\begin{spfstep}
51+
As $G$ is \termref[cd=balanced-binary-trees,name=balanced-binary-tree]{balanced} and $\gdepth{G}=n>i$, $\livar{V}{i-1}$ cannot contain
52+
leaves.
53+
\end{spfstep}
54+
\begin{spfstep}[type=conclusion]
55+
Thus $\eq{\card{\livar{V}i},{\atimes[cdot]{2,\card{\livar{V}{i-1}}}},{\atimes[cdot]{2,\power2{i-1}}},\power2i}$.
56+
\end{spfstep}
57+
\end{spfcase}
58+
\end{spfcases}
59+
\end{sproof}
60+
\item
61+
\begin{assertion}[id=fbbt,type=corollary]
62+
A fully balanced tree of depth $d$ has $\power2{d+1}-1$ nodes.
63+
\end{assertion}
64+
\item
65+
\begin{sproof}[for=fbbt,id=fbbt-pf]{}
66+
\begin{spfstep}
67+
Let $\defeq{G}{\tup{V,E}}$ be a fully balanced tree
68+
\end{spfstep}
69+
\begin{spfstep}
70+
Then $\card{V}=\Sumfromto{i}1d{\power2i}= \power2{d+1}-1$.
71+
\end{spfstep}
72+
\end{sproof}
73+
\end{itemize}
74+
\end{frame}
75+
\begin{note}
76+
\begin{omtext}[type=conclusion,for=binary-tree]
77+
This shows that balanced binary trees grow in breadth very quickly, a consequence of
78+
this is that they are very shallow (and this compute very fast), which is the essence of
79+
the next result.
80+
\end{omtext}
81+
\end{note}
82+
\end{module}
83+
84+
%%% Local Variables:
85+
%%% mode: LaTeX
86+
%%% TeX-master: "all"
87+
%%% End: \end{document}
88+
</textarea></form>
89+
<script>
90+
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {});
91+
</script>
92+
93+
<p><strong>MIME types defined:</strong> <code>text/stex</code>.</p>
94+
95+
</body>
96+
</html>

mode/stex/stex.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
span.css-at {color: #708;}
2+
span.css-unit {color: #281;}
3+
span.css-value {color: #708;}
4+
span.css-identifier {color: black;}
5+
span.css-selector {color: #11B;}
6+
span.css-important {color: #00F;}
7+
span.css-colorcode {color: #299;}
8+
span.css-comment {color: #A70;}
9+
span.css-string {color: #A22;}
10+
11+
span.stex-unit { color: #281; }
12+
span.stex-identifier { color: black; }
13+
span.stex-slash { color: #FAA; }
14+
span.stex-command { color: #00F; }
15+
span.stex-comment { color: #A70; }
16+
span.stex-import { color: #00F; }
17+
span.stex-filepath { color: #852626; }
18+
span.stex-module { color: #852626; }
19+
span.stex-error { text-decoration: underline; color: red; }
20+
span.stex-string { color: #A22; }

mode/stex/stex.js

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* Author: Constantin Jucovschi ([email protected])
3+
* Licence: MIT
4+
*/
5+
6+
CodeMirror.defineMode("stex", function(cmCfg, modeCfg)
7+
{
8+
function pushCommand(state, command) {
9+
state.cmdState.push(command);
10+
}
11+
12+
function peekCommand(state) {
13+
if (state.cmdState.length>0)
14+
return state.cmdState[state.cmdState.length-1];
15+
else
16+
return null;
17+
}
18+
19+
function popCommand(state) {
20+
if (state.cmdState.length>0) {
21+
var plug = state.cmdState.pop();
22+
plug.closeBracket();
23+
}
24+
}
25+
26+
function applyMostPowerful(state) {
27+
context = state.cmdState;
28+
for (var i = context.length - 1; i >= 0; i--) {
29+
var plug = context[i];
30+
if (plug.name=="DEFAULT")
31+
continue;
32+
return plug.styleIdentifier();
33+
}
34+
return "stex-identifier";
35+
}
36+
37+
function addPluginPattern(pluginName, cmdStyle, brackets, styles) {
38+
return function () {
39+
this.name=pluginName;
40+
this.bracketNo = 0;
41+
this.style=cmdStyle;
42+
this.styles = styles;
43+
this.brackets = brackets;
44+
45+
this.styleIdentifier = function(content) {
46+
if (this.bracketNo<=this.styles.length)
47+
return this.styles[this.bracketNo-1];
48+
else
49+
return null;
50+
};
51+
this.openBracket = function(content) {
52+
this.bracketNo++;
53+
return "stex-bracket";
54+
};
55+
this.closeBracket = function(content) {
56+
};
57+
}
58+
}
59+
60+
var plugins = new Array();
61+
62+
plugins["importmodule"] = addPluginPattern("importmodule", "stex-command", "{[", ["stex-filepath", "stex-module"]);
63+
plugins["documentclass"] = addPluginPattern("documentclass", "stex-command", "{[", ["", "stex-unit"]);
64+
plugins["usepackage"] = addPluginPattern("documentclass", "stex-command", "[", ["stex-unit"]);
65+
plugins["begin"] = addPluginPattern("documentclass", "stex-command", "[", ["stex-unit"]);
66+
plugins["end"] = addPluginPattern("documentclass", "stex-command", "[", ["stex-unit"]);
67+
68+
plugins["DEFAULT"] = function () {
69+
this.name="DEFAULT";
70+
this.style="stex-command";
71+
72+
this.styleIdentifier = function(content) {
73+
};
74+
this.openBracket = function(content) {
75+
};
76+
this.closeBracket = function(content) {
77+
};
78+
};
79+
80+
function setState(state, f) {
81+
state.f = f;
82+
}
83+
84+
function normal(source, state) {
85+
if (source.match(/^\\[a-z]+/)) {
86+
cmdName = source.current();
87+
cmdName = cmdName.substr(1, cmdName.length-1);
88+
var plug = plugins[cmdName];
89+
if (typeof(plug) == 'undefined') {
90+
plug = plugins["DEFAULT"];
91+
}
92+
plug = new plug();
93+
pushCommand(state, plug);
94+
setState(state, beginParams);
95+
return plug.style;
96+
}
97+
98+
var ch = source.next();
99+
if (ch == "%") {
100+
setState(state, inCComment);
101+
return "stex-comment";
102+
}
103+
else if (ch=='}' || ch==']') {
104+
plug = peekCommand(state);
105+
if (plug) {
106+
plug.closeBracket(ch);
107+
setState(state, beginParams);
108+
} else
109+
return "stex-error";
110+
return "stex-bracket";
111+
} else if (ch=='{' || ch=='[') {
112+
plug = plugins["DEFAULT"];
113+
plug = new plug();
114+
pushCommand(state, plug);
115+
return "stex-bracket";
116+
}
117+
else if (/\d/.test(ch)) {
118+
source.eatWhile(/[\w.%]/);
119+
return "stex-unit";
120+
}
121+
else {
122+
source.eatWhile(/[\w-_]/);
123+
return applyMostPowerful(state);
124+
}
125+
}
126+
127+
function inCComment(source, state) {
128+
source.skipToEnd();
129+
setState(state, normal);
130+
return "css-comment";
131+
}
132+
133+
function beginParams(source, state) {
134+
var ch = source.peek();
135+
if (ch == '{' || ch == '[') {
136+
lastPlug = peekCommand(state);
137+
style = lastPlug.openBracket(ch);
138+
source.eat(ch);
139+
setState(state, normal);
140+
return "stex-bracket";
141+
}
142+
if (/[ \t\r]/.test(ch)) {
143+
source.eat(ch);
144+
return null;
145+
}
146+
setState(state, normal);
147+
lastPlug = peekCommand(state);
148+
if (lastPlug) {
149+
popCommand(state);
150+
}
151+
return normal(source, state);
152+
}
153+
154+
return {
155+
startState: function() { return { f:normal, cmdState:[] }; },
156+
copyState: function(s) { return { f: s.f, cmdState: s.cmdState.slice(0, s.cmdState.length) }; },
157+
158+
token: function(stream, state) {
159+
var t = state.f(stream, state);
160+
var w = stream.current();
161+
return t;
162+
}
163+
};
164+
});
165+
166+
167+
CodeMirror.defineMIME("text/x-stex", "stex");

0 commit comments

Comments
 (0)