-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[CS2] Literate CoffeeScript without dependencies #4509
Changes from 1 commit
b637592
44049ae
6d488f1
24c6c21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,6 @@ | |
# the **Lexer**, **Rewriter**, and the **Nodes**. Merge objects, flatten | ||
# arrays, count characters, that sort of thing. | ||
|
||
md = require('markdown-it')() | ||
|
||
# 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 | ||
|
@@ -69,20 +67,54 @@ exports.some = Array::some ? (fn) -> | |
return true for e in this when fn e | ||
false | ||
|
||
# Simple function for extracting code from Literate CoffeeScript by stripping | ||
# Helper 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.” Uses [MarkdownIt](https://markdown-it.github.io/) | ||
# to tell the difference between Markdown and code blocks. | ||
# be compiled “normally.” | ||
exports.invertLiterate = (code) -> | ||
out = [] | ||
md.renderer.rules = | ||
# Delete all other rules, since all we want are the code blocks. | ||
code_block: (tokens, idx, options, env, slf) -> | ||
startLine = tokens[idx].map[0] | ||
lines = tokens[idx].content.split '\n' | ||
for line, i in lines | ||
out[startLine + i] = line | ||
md.render code | ||
blankLine = /^\s*$/ | ||
indented = /^[\t ]/ | ||
listItemStart = /// ^ | ||
(?:\t?|\ {0,3}) # Up to one tab, or up to three spaces, or neither; | ||
(?: | ||
[\*\-\+] | # followed by `*`, `-` or `+`; | ||
[0-9]{1,9}\. # or by an integer up to 9 digits long, followed by a period; | ||
) | ||
[\ \t] # followed by a space or a tab. | ||
/// | ||
listItemFirstCharacter = new RegExp "#{listItemStart.source}+(\\S)" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be written as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, why is |
||
insideComment = no | ||
insideList = no | ||
indentation = 0 | ||
for line in code.split('\n') | ||
if blankLine.test(line) | ||
insideComment = no | ||
out.push line | ||
else if listItemStart.test(line) | ||
insideList = yes | ||
# Get the first non-whitespace character after the bullet or period, to | ||
# determine our new base indentation level. | ||
indentation = line.indexOf line.match(listItemFirstCharacter)[1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't this crash if the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we are looking for something more like: match = listItemStart.exec line
if match
indentation = line[match[0].length..].search /\S/
# TODO: `.search` returns `-1` if nothing could be found |
||
out.push "# #{line}" | ||
else if indented.test(line) | ||
if insideComment | ||
out.push "# #{line}" | ||
else if insideList | ||
# Are we in a list, and indented relative to the indentation of the list? | ||
# If so, treat as code. | ||
if indented.test(line.substring(indentation)) | ||
out.push line.substring(indentation) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In CoffeeScript I'd say it's more common to use slicing syntax. |
||
# Otherwise this is part of a list item paragraph. | ||
else | ||
out.push "# #{line}" | ||
else | ||
insideComment = no | ||
out.push line | ||
else | ||
insideComment = yes | ||
insideList = no | ||
indentation = 0 | ||
out.push "# #{line}" | ||
out.join '\n' | ||
|
||
# Merge two jison-style location data objects together. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious where you got this "up to 9 digits long" rule from? :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
http://spec.commonmark.org/0.27/#list-items