Markdown reader separators and foreword

- make separators configurable
- if first block is separator, add an empty foreword
This commit is contained in:
Vit Brunner
2013-06-17 23:47:03 +01:00
parent 46b3033e2a
commit 73cea13621
2 changed files with 50 additions and 13 deletions
@@ -24,26 +24,31 @@ angular.module("sideBySide").factory("markdownReader", () ->
#
# @param lexed [Array] Lexed blocks
# @return [Array] Section
readSection = (lexed) ->
readSection = (lexed, separators) ->
items = []
while lexed.length > 0
break if lexed[0]["type"] == "heading"
break if lexed[0]["type"] in separators
items.push(lexed.shift())
return items
# Read content from lexed blocks
#
# @param lexed [Array] Lexed blocks
# @param separators [Array] Section separators
# @return [Array] Content
readContent = (lexed) ->
readContent = (lexed, separators) ->
content = []
while lexed.length > 0
heading = if lexed[0]["type"] == "heading" \
then lexed.shift().text \
else heading = ''
# if first block is separator, add dummy foreword
if lexed[0]["type"] in separators
lexed.unshift({type: "paragraph", text: ""})
text = readSection(lexed)
while lexed.length > 0
heading = if lexed[0]["type"] in separators \
then lexed.shift().text or "" \
else ""
text = readSection(lexed, separators)
text.links = lexed.links
content.push({
section: heading
@@ -54,9 +59,11 @@ angular.module("sideBySide").factory("markdownReader", () ->
lexed = marked.lexer(source)
meta = lexed.shift()
meta = readMeta(meta.text, new marked.InlineLexer(lexed.links))
separators = if meta.Separator \
then [meta.Separator] else ['heading', 'hr']
return {
meta: meta
content: readContent(lexed)
content: readContent(lexed, separators)
}
)
@@ -35,6 +35,7 @@ test("markdownReader", () ->
Source: '<a href="http://en.wikisource.org/wiki/Universal_Declaration_of_Human_Rights">http://en.wikisource.org/wiki/Universal_Declaration_of_Human_Rights</a>'
}
content: [
{ section: "", text: "<p></p>\n" }
{ section: "1", text: "<p>All human beings are born <em>free</em> and equal in dignity and rights...</p>\n" }
{ section: "2", text: """
<p>Everyone is entitled to all the rights and freedoms...</p>
@@ -52,21 +53,50 @@ test("markdownReader", () ->
}
}, {
tested: """
Title: Test leading text
Title: Test leading text and mixed separators
**This** is leading text.
# I
First section.
---
Second section.
"""
expected: {
meta: {
Title: "Test leading text and mixed separators"
}
content: [
{ section: "", text: "<p><strong>This</strong> is leading text.</p>\n" }
{ section: "I", text: "<p>First section.</p>\n" }
{ section: "", text: "<p>Second section.</p>\n" }
]
}
}, {
tested: """
Title: Separator test
Separator: heading
# I
First section.
---
Second part.
# II
Second section.
"""
expected: {
meta: {
Title: "Test leading text"
Title: "Separator test"
Separator: "heading"
}
content: [
{ section: "", text: "<p><strong>This</strong> is leading text.</p>\n" }
{ section: "I", text: "<p>First section.</p>\n" }
{ section: "", text: "<p></p>\n" }
{ section: "I", text: """
<p>First section.</p>
<hr>
<p>Second part.</p>\n""" }
{ section: "II", text: "<p>Second section.</p>\n" }
]
}