Commits

firefly committed c153c10

Actually renamed wikidown→wykedown. Added strikeout syntax: ~foo~.

  • Participants
  • Parent commits 10eb1ba

Comments (0)

Files changed (2)

src/wikidown.coffee

-{markdown} = require '../deps/markdown'
-{Markdown} = markdown
-
-wikidown = Markdown.subclassDialect Markdown.dialects.Gruber
-
-#### Templates: {{Foo|bar|baz=qux}} #######################
-wikidown.inline['{{'] = (text) ->
-	res = @processInline text[2..]
-	for el,idx in res
-		if el.name == 'close_template'
-			content = res[0...idx]
-			rest    = res[idx+1..]
-			
-			# Process template arguments
-			args    = []
-			currArg = []
-			
-			for el in content
-				if typeof el == 'string'
-					[first, trailing ...] = el.split "|"
-					currArg.push first
-					
-					for part in trailing
-						args.push currArg
-						currArg = []
-						currArg.push part if part != ""
-				
-				else
-					currArg.push el
-			
-			args.push currArg if currArg.length
-			
-			name = args.shift()
-			if name.length != 1 or typeof name[0] != 'string'
-				throw new Error "Invalid template name. #{JSON.stringify name}"
-			
-			props = {
-				name : name[0]
-				args : args
-			}
-			
-			return [ text.length, ['template', props] ].concat rest
-			#return [ text.length, ['template', '<FIXME>'] ].concat rest
-	
-	# Shouldn't get here
-	console.log "Warn: Couldn't match template opening tag."
-	[ 2, '{{' ]	# Fall back gracefully to doing nothing...
-
-wikidown.inline['}}'] = (text) ->
-	[ 2, {
-		name: 'close_template'
-	} ]
-
-#### Wikilinks: [[foo]] or [[foo|bar]] ####################
-wikidown.inline['[['] = (text) ->
-	match = text.match ///
-		^\[\[				# start with literal [[
-		( [^ \| \] ]+ )		# non-]-or-| characters
-		(?:					# optionally,
-			\|				# literal |
-			( [^ \] ]* )	# followed by non-] characters
-		)?					# 
-		\]\]				# literal ]]
-		///
-	
-	if not match
-		throw new Error "Parse error: couldn't find end of wikilink starting with '#{text[0..30]}'."
-	
-	[wholeMatch, target, display] = match
-	display ?= target
-	
-	[ wholeMatch.length, [ 'wikilink', { target: target }, display ] ]
-
-#### Special symbols syntax ###############################
-wikidown.inline['--']  = -> [ 2, "&#8211;" ]	# en dash (from TeX)
-wikidown.inline['---'] = -> [ 2, "&#8212;" ]	# em dash (from TeX)
-wikidown.inline['->']  = -> [ 2, "&#8594;" ]	# rightwards arrow
-wikidown.inline['=>']  = -> [ 2, "&#8658;" ]	# rightwards double arrow
-wikidown.inline['<-']  = -> [ 2, "&#8592;" ]	# leftwards arrow
-wikidown.inline['<=']  = -> [ 2, "&#8656;" ]	# leftwards double arrow
-
-# Copy over all keys (to fix precedence)
-for key,val of Markdown.dialects.Gruber.block
-	wikidown.block[key] = val
-
-#### Fix paragraphs containing HTML #######################
-wikidown.block['para'] = (block, next) ->
-	# Everything's a para! Well, almost...
-	# count newlines & lines that are HTML-ish
-	lineCount     = block.split('\n').length
-#	htmlLineCount = block.match(/^\s*(?:<.*>\s*.*|)?$/gm)?.length
-	htmlLineCount = block.match(/^\s*<.*>\s*$/gm)?.length
-	
-	if lineCount == htmlLineCount
-		return [ ["raw", block.toString()] ]	# Don't mess with HTML!
-	
-	[ ["para"].concat @processInline block ]
-
-#### Other stuff ##########################################
-# TODO: Clean this portion up.
-Markdown.buildBlockOrder     wikidown.block
-Markdown.buildInlinePatterns wikidown.inline
-
-mapTree = (node, pred, callback) ->
-	if typeof node == 'string'
-		return node
-	
-	attrs = {}
-	[name, children] = node
-	
-	if children not instanceof Array
-		[_, attrs, children] = node
-	
-	shouldReplace = pred name, attrs, children
-	
-	if shouldReplace
-		callback name, attrs, children
-	else
-		newChildren = (mapTree ch, pred, callback for ch in children)
-		
-		if Object.keys(attrs).length > 0
-			[name, attrs, newChildren]
-		else
-			[name, newChildren]
-
-#documents =
-#	"Foo" : """
-#	Template substitution test: {{Bar|*foo*}}
-#	"""
-#	"Bar" : "Yeah! {{:0}}"
-
-getFormatted = (name, args) ->
-	res = markdown.parse documents[name], wikidown
-	
-	newRes = mapTree res, ((name) -> name == "template"), (name, attrs, children) ->
-		if attrs["name"][0] == ':'
-			return [ "div" ].concat args[attrs["name"][1..]]
-		
-		[name, children] = getFormatted attrs["name"], attrs["args"]
-		children
-	
-	newRes
-
-exports.wikidown = wikidown
-exports.parse    = (source) -> markdown.parse  source, wikidown
-exports.toHTML   = (source) -> markdown.toHTML source, wikidown
-exports.utils    = {
-	mapTree : mapTree
-}
-
-#res = getFormatted "Foo", {}
-#console.log require('util').inspect res, null, null
-#console.log markdown.toHTML res

src/wykedown.coffee

+{markdown} = require '../deps/markdown'
+{Markdown} = markdown
+
+wykedown = Markdown.subclassDialect Markdown.dialects.Gruber
+
+#### Templates: {{Foo|bar|baz=qux}} #######################
+wykedown.inline['{{'] = (text) ->
+	res = @processInline text[2..]
+	for el,idx in res
+		if el.name == 'close_template'
+			content = res[0...idx]
+			rest    = res[idx+1..]
+			
+			# Process template arguments
+			args    = []
+			currArg = []
+			
+			for el in content
+				if typeof el == 'string'
+					[first, trailing ...] = el.split "|"
+					currArg.push first
+					
+					for part in trailing
+						args.push currArg
+						currArg = []
+						currArg.push part if part != ""
+				
+				else
+					currArg.push el
+			
+			args.push currArg if currArg.length
+			
+			name = args.shift()
+			if name.length != 1 or typeof name[0] != 'string'
+				throw new Error "Invalid template name. #{JSON.stringify name}"
+			
+			props = {
+				name : name[0]
+				args : args
+			}
+			
+			return [ text.length, ['template', props] ].concat rest
+	
+	# Shouldn't get here
+	console.log "Warn: Couldn't match template opening tag."
+	[ 2, '{{' ]	# Fall back gracefully to doing nothing...
+
+wykedown.inline['}}'] = (text) ->
+	[ 2, {
+		name: 'close_template'
+	} ]
+
+#### Wikilinks: [[foo]] or [[foo|bar]] ####################
+wykedown.inline['[['] = (text) ->
+	match = text.match ///
+		^\[\[				# start with literal [[
+		( [^ \| \] ]+ )		# non-]-or-| characters
+		(?:					# optionally,
+			\|				# literal |
+			( [^ \] ]* )	# followed by non-] characters
+		)?					# 
+		\]\]				# literal ]]
+		///
+	
+	if not match
+		throw new Error "Parse error: couldn't find end of wikilink starting with '#{text[0..30]}'."
+	
+	[wholeMatch, target, display] = match
+	display ?= target
+	
+	[ wholeMatch.length, [ 'wikilink', { target: target }, display ] ]
+
+#### Strikeout: ~foo~ #####################################
+wykedown.inline['~'] = (text) ->
+	index = text[1..].indexOf '~'
+
+	if not ~index
+		return null
+	
+	content = text[1..index]
+	
+	[ index + 2, ['del', content] ]
+
+#### Special symbols syntax ###############################
+wykedown.inline['--']  = -> [ 2, "&#8211;" ]	# en dash (from TeX)
+wykedown.inline['---'] = -> [ 3, "&#8212;" ]	# em dash (from TeX)
+wykedown.inline['->']  = -> [ 2, "&#8594;" ]	# rightwards arrow
+wykedown.inline['=>']  = -> [ 2, "&#8658;" ]	# rightwards double arrow
+wykedown.inline['<-']  = -> [ 2, "&#8592;" ]	# leftwards arrow
+wykedown.inline['<=']  = -> [ 2, "&#8656;" ]	# leftwards double arrow
+
+# Copy over all keys (to fix precedence)
+for key,val of Markdown.dialects.Gruber.block
+	wykedown.block[key] = val
+
+#### Fix paragraphs containing HTML #######################
+wykedown.block['para'] = (block, next) ->
+	# Everything's a para! Well, almost...
+	# count newlines & lines that are HTML-ish
+	lineCount     = block.split('\n').length
+#	htmlLineCount = block.match(/^\s*(?:<.*>\s*.*|)?$/gm)?.length
+	htmlLineCount = block.match(/^\s*<.*>\s*$/gm)?.length
+	
+	if lineCount == htmlLineCount
+		return [ ["raw", block.toString()] ]	# Don't mess with HTML!
+	
+	[ ["para"].concat @processInline block ]
+
+#### Other stuff ##########################################
+# TODO: Clean this portion up.
+Markdown.buildBlockOrder     wykedown.block
+Markdown.buildInlinePatterns wykedown.inline
+
+mapTree = (node, pred, callback) ->
+	if typeof node == 'string'
+		return node
+	
+	attrs = {}
+	[name, children] = node
+	
+	if children not instanceof Array
+		[_, attrs, children] = node
+	
+	shouldReplace = pred name, attrs, children
+	
+	if shouldReplace
+		callback name, attrs, children
+	else
+		newChildren = (mapTree ch, pred, callback for ch in children)
+		
+		if Object.keys(attrs).length > 0
+			[name, attrs, newChildren]
+		else
+			[name, newChildren]
+
+#documents =
+#	"Foo" : """
+#	Template substitution test: {{Bar|*foo*}}
+#	"""
+#	"Bar" : "Yeah! {{:0}}"
+
+getFormatted = (name, args) ->
+	res = markdown.parse documents[name], wykedown
+	
+	newRes = mapTree res, ((name) -> name == "template"), (name, attrs, children) ->
+		if attrs["name"][0] == ':'
+			return [ "div" ].concat args[attrs["name"][1..]]
+		
+		[name, children] = getFormatted attrs["name"], attrs["args"]
+		children
+	
+	newRes
+
+exports.wykedown = wykedown
+exports.parse    = (source) -> markdown.parse  source, wykedown
+exports.toHTML   = (source) -> markdown.toHTML source, wykedown
+exports.utils    = {
+	mapTree : mapTree
+}
+
+#res = getFormatted "Foo", {}
+#console.log require('util').inspect res, null, null
+#console.log markdown.toHTML res