Skip to content

Instantly share code, notes, and snippets.

@tiye
Created September 8, 2012 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiye/3673285 to your computer and use it in GitHub Desktop.
Save tiye/3673285 to your computer and use it in GitHub Desktop.
bad markHTML
<a>
<a>
<a>
<a>
<a>
<a>
<a>
<a>
<a>
<a>
```
<a>
<a>
````
<a>
<a>
```
<a>
```
var backquote, indented, markLine;
indented = function(line) {
return line.slice(0, 4) === ' ';
};
backquote = function(line) {
return line.slice(0, 3) === '```';
};
markLine = function(line) {
if (line[0] === '>') {
return mark_line(line.slice(1));
} else {
return line.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\s/g, '&nbsp;');
}
};
exports.html = function(str) {
var isQuote, isText, list;
isText = true;
isQuote = false;
list = [];
str.split('\n').forEach(function(line) {
var ret;
if (indented(line)){isText = false }else{isText = true};
if (backquote(line)) isQuote = !isQuote;
ret = isText && (!isQuote) ? markLine(line) : line;
return list.push(ret);
});
return list.join('\n');
};
html = require('./markHTML').html
fs = require 'fs'
raw = fs.readFileSync 'file.txt', 'utf8'
out = html raw
list_a = raw.split '\n'
list_b = out.split '\n'
len = list_b.length
show = console.log
[0...len].forEach (i) ->
show i, list_a[i]
show i, list_b[i]
show '\n'
➤➤ coffee t.coffee
0 ''
0 ''
1 '<a>'
1 '&lt;a&gt;'
2 ' <a>'
2 ' <a>'
3 '<a>'
3 '&lt;a&gt;'
4 ' <a>'
4 '&nbsp;&nbsp;&lt;a&gt;'
5 ' <a>'
5 ' <a>'
6 ' <a>'
6 ' <a>'
7 ' <a>'
7 ' <a>'
8 ' <a>'
8 ' <a>'
9 ' <a>'
9 '&nbsp;&nbsp;&lt;a&gt;'
10 '<a>'
10 '&lt;a&gt;'
11 '``` '
11 '``` '
12 '<a>'
12 '<a>'
13 ' <a>'
13 ' <a>'
14 '````'
14 '````'
15 ' <a>'
15 '&nbsp;&nbsp;&lt;a&gt;'
16 '<a>'
16 '&lt;a&gt;'
17 '```'
17 '```'
18 '<a>'
18 '<a>'
19 '```'
19 '```'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment