Move source/documents/* to source/

This commit is contained in:
Vit Brunner
2014-10-29 12:19:07 +01:00
parent 6c4d6bda84
commit f4b77f6215
31 changed files with 1 additions and 1 deletions
@@ -0,0 +1,15 @@
angular.module("sideBySide").factory "readerFactory", ['$injector', ($injector) ->
# Create poem reader based on file name
#
# @param [String] Source file name
# @return [Object] Reader
(filename) ->
extension = filename.split(".").pop()
switch extension
when "json"
return $injector.get("jsonReader")
when "md", "markdown"
return $injector.get("markdownReader")
else
throw "Unknown extension '" + extension + "'."
]
@@ -0,0 +1,20 @@
angular.module("sideBySide").factory("jsonReader", () ->
# Read json poem
#
# For sample input, see:
# source/documents/tests/services/reader/json.js.coffee
#
# @param source [String] Json poem
# @return [Object] Poem object
(source) ->
parsed = eval('(' + source + ')')
for i,verse of parsed.content
if typeof(verse) == "string"
parsed.content[i] = { section: "", text: verse }
else
verse.section = "" if "section" not of verse
parsed.content[i] = verse
parsed
)
@@ -0,0 +1,66 @@
angular.module("sideBySide").factory("markdownReader", () ->
# Read markdown poem
#
# For sample input, see:
# source/documents/tests/services/reader/markdown.js.coffee
#
# @param source [String] Markdown poem
# @return [Object] Poem object
(source) ->
# Read meta information from markdown block
#
# @param block [string] Meta block
# @param inlineLexer [Object] Inline markdown lexer
# @return [Object] Meta properties
readMeta = (text, inlineLexer) ->
meta = {}
for line in text.split("\n")
lexed = inlineLexer.output(line)
match = /([^:]*):(.*)/.exec(lexed)
meta[match[1].trim()] = match[2].trim()
meta
# Read and remove one section from start of lexed blocks
#
# @param lexed [Array] Lexed blocks
# @return [Array] Section
readSection = (lexed, separators) ->
items = []
while lexed.length > 0
break if lexed[0]["type"] in separators
items.push(lexed.shift())
items
# Read content from lexed blocks
#
# @param lexed [Array] Lexed blocks
# @param separators [Array] Section separators
# @return [Array] Content
readContent = (lexed, separators) ->
content = []
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
text: marked.parser(text)
})
content
marked.setOptions({ smartypants: true })
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']
{
meta: meta
content: readContent(lexed, separators)
}
)