Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use markdown parser to identify code blocks in literate coffeescript #3924

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"preferGlobal": true,
"scripts": {
"test": "node ./bin/cake test",
"test": "node ./bin/cake test",
"test-harmony": "node --harmony ./bin/cake test"
},
"homepage": "http://coffeescript.org",
Expand All @@ -38,5 +38,8 @@
"highlight.js": "~8.0.0",
"underscore": "~1.5.2",
"docco": "~0.7.0"
},
"dependencies": {
"marked": "~0.3.3"
}
}
38 changes: 26 additions & 12 deletions src/helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
# the **Lexer**, **Rewriter**, and the **Nodes**. Merge objects, flatten
# arrays, count characters, that sort of thing.

marked = require 'marked'
# marked.setOptions
# renderer: new marked.Renderer()
# gfm: true
# tables: true
# breaks: false
# pedantic: false
# sanitize: true
# smartLists: true
# smartypants: false

# Peek at the beginning of a given string to see if it matches a sequence.
exports.starts = (string, literal, start) ->
literal is string.substr start, literal.length
Expand Down Expand Up @@ -67,19 +78,22 @@ exports.some = Array::some ? (fn) ->
return true for e in this when fn e
false

# Simple function for inverting Literate CoffeeScript code by putting the
# documentation in comments, producing a string of CoffeeScript code that
# can be compiled "normally".
# Simple function for extracting code from Literate CoffeeScript by stripping
# out all non-code blocks, producing a string of CoffeeScript code that can
# be compiled "normally".
exports.invertLiterate = (code) ->
maybe_code = true
lines = for line in code.split('\n')
if maybe_code and /^([ ]{4}|[ ]{0,3}\t)/.test line
line
else if maybe_code = /^\s*$/.test line
line
else
'# ' + line
lines.join '\n'
# don't know how to avoid this hack using token as placeholder for tabs, then
# re-inserting tabs after code extraction. The token has been split in two
# so that it does not end up getting parsed in this src code
token = '9ddb1d26184bdaf8'+'d4de55835d82eb56'
code = code.replace "\t", token
# parse as markdown, discard everything except code blocks
out = ""
for item in marked.lexer code, {}
out += "#{item.text}\n" if item.type == 'code'
# put the tabs back in
out.replace token, "\t"
out

# Merge two jison-style location data objects together.
# If `last` is not provided, this will simply return `first`.
Expand Down
108 changes: 108 additions & 0 deletions test/literate.litcoffee
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,111 @@ Tabs work too:

test "tabbed code", ->
ok yes

---

# keep track of whether code blocks are executed or not
executed = false

<p>

executed = true # should not execute, this is just HTML para, not code!

</p>

test "should ignore indented sections inside HTML", ->
eq executed, false

---

* A list item with a code block:

test "basic literate CoffeeScript parsing", ->
ok yes

---

* Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
viverra nec, fringilla in, laoreet vitae, risus.

* Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
Suspendisse id sem consectetuer libero luctus adipiscing.

---

1. This is a list item with two paragraphs. Lorem ipsum dolor
sit amet, consectetuer adipiscing elit. Aliquam hendrerit
mi posuere lectus.

Vestibulum enim wisi, viverra nec, fringilla in, laoreet
vitae, risus. Donec sit amet nisl. Aliquam semper ipsum
sit amet velit.

2. Suspendisse id sem consectetuer libero luctus adipiscing.

---

1. This is a list item with two paragraphs. Lorem ipsum dolor
sit amet, consectetuer adipiscing elit. Aliquam hendrerit
mi posuere lectus.

Vestibulum enim wisi, viverra nec, fringilla in, laoreet
vitae, risus. Donec sit amet nisl. Aliquam semper ipsum
sit amet velit.

2. Suspendisse id sem consectetuer libero luctus adipiscing.

---

* A list item with a blockquote:

> This is a blockquote
> inside a list item.

---

This next one probably passes because a string is inoffensive in compiled js, also, can't get `marked` to parse it correctly, and not sure if empty line is permitted between title and reference

This is [an example][id] reference-style link.
[id]: http://example.com/

"Optional Title Here"

---

executed = no

1986. What a great season.
executed = yes

and test...

test "should recognise indented code blocks in lists", ->
ok executed

---

executed = no

1986. What a great season.

executed = yes

and test...

test "should recognise indented code blocks in lists with empty line as separator", ->
ok executed

---

executed = no

1986\. What a great season.
executed = yes

and test...

test "should ignore indented code in escaped list like number", ->
eq executed, no