Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #43 from jamesramsay/escape-quotes
Browse files Browse the repository at this point in the history
Escaped quotes and precompiled pegjs grammar
  • Loading branch information
James Ramsay committed Jul 22, 2015
2 parents 2ece19b + e15dcc9 commit ebbd653
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 87 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hercule",
"version": "1.2.0",
"version": "1.2.1",
"description": "Markdown transcluder",
"main": "./lib/hercule",
"scripts": {
Expand All @@ -9,8 +9,8 @@
"integration-tests": "npm run compile && ./test/modules/bin/bats test/bats",
"coverage": "NODE_ENV=test COVERAGE=1 ./node_modules/.bin/mocha --compilers coffee:coffee-script/register --require blanket --reporter html-cov > coverage.html && open coverage.html",
"coveralls": "NODE_ENV=test COVERAGE=1 ./node_modules/.bin/mocha --compilers coffee:coffee-script/register --require blanket --reporter mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js",
"compile": "coffee --output lib -c src/*",
"watch": "coffee --output lib -cw src/*",
"compile": "coffee --output lib -c src/*.coffee && ./node_modules/pegjs/bin/pegjs src/transclude.pegjs lib/transclude-parser.js",
"watch": "coffee --output lib -cw src/*.coffee",
"install-bats": "./scripts/install-bats"
},
"config": {
Expand Down Expand Up @@ -46,7 +46,6 @@
"async": "^1.3.0",
"dashdash": "^1.7.0",
"lodash": "^3.0.0",
"pegjs": "^0.8.0",
"request": "^2.58.0"
},
"devDependencies": {
Expand All @@ -56,6 +55,7 @@
"coveralls": "^2.11.2",
"mocha": "^2.2.5",
"mocha-lcov-reporter": "^0.0.2",
"nock": "^2.7.0"
"nock": "^2.7.0",
"pegjs": "^0.8.0"
}
}
76 changes: 0 additions & 76 deletions src/grammar.coffee

This file was deleted.

76 changes: 76 additions & 0 deletions src/transclude.pegjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
start = l:link? " || "? d:default? " "? o:reference* {
return {
"href": l.href,
"hrefType": l.hrefType,
"references": o,
"default": d
};
}

reference = p:placeholder ":" l:link " "? {
return {
"placeholder": p,
"href": l.href,
"hrefType": l.hrefType
};
}

placeholder = p:[a-zA-Z0-9]+ {
return p.join("");
}

default = httpLink / fileLink / stringLink

link = httpLink / fileLink / stringLink / reset

fileLink = f:[^ ()\"]+ {
return {
"hrefType": "file",
"href": f.join("")
};
}

httpLink = left:("http://" / "https://") right:[^ ()]+ {
return {
"hrefType": "http",
"href": left + right.join("")
};
}

stringLink = s:string {
return {
"hrefType": "string",
"href": s
};
}

reset = {
return {
"hrefType": "string",
"href": ""
};
}

string = QUOTATION_MARK chars:char* QUOTATION_MARK {
return chars.join("");
}

char = UNESCAPED / ESCAPE sequence:(
"\""
/ "\\"
/ "/"
/ "b" { return "\b"; }
/ "f" { return "\f"; }
/ "n" { return "\n"; }
/ "r" { return "\r"; }
/ "t" { return "\t"; }
/ "u" digits:$(HEXDIG HEXDIG HEXDIG HEXDIG) {
return String.fromCharCode(parseInt(digits, 16));
}
)
{ return sequence; }

ESCAPE = "\\"
QUOTATION_MARK = "\""
UNESCAPED = [\x20-\x21\x23-\x5B\x5D-\u10FFFF]
HEXDIG = [0-9a-f]i
12 changes: 6 additions & 6 deletions src/utils.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
fs = require 'fs'
path = require 'path'
peg = require 'pegjs'
_ = require 'lodash'
request = require 'request'

try
linkParser = require './transclude-parser'
catch ex
peg = require 'pegjs'
linkParser = peg.buildParser (fs.readFileSync './src/transclude.pegjs', {encoding: 'utf8'})


# Link detection (including leading whitespace)
linkRegExp = new RegExp(/(^[\t ]*)?(\:\[.*?\]\((.*?)\))/gm)
Expand All @@ -12,11 +17,6 @@ PLACEHOLDER_GROUP = 2
LINK_GROUP = 3


# Build the link parser once
grammar = require './grammar'
linkParser = peg.buildParser grammar.transcludeGrammar


# Scan a document string for links
scan = (input, relativePath = "", parents = []) ->
links = []
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/test-quotes/foo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"bar": :[test](barValue || "null")
}
4 changes: 4 additions & 0 deletions test/fixtures/test-quotes/main.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```
:[example](foo.json),
:[example](foo.json barValue:"\"green\"")
```
16 changes: 16 additions & 0 deletions test/hercule.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,19 @@ describe 'hercule', ->
assert.equal output, 'Jackdaws love my imagined sphinx of quartz.\n'
done()

it 'should transclude files with escaped quotes within strings', (done) ->
inputFile = __dirname + "/fixtures/test-quotes/main.md"

hercule.transcludeFile inputFile, (output) ->
assert.equal output, """
```
{
"bar": null
},
{
"bar": "green"
}
```
"""
done()

0 comments on commit ebbd653

Please sign in to comment.