Skip to content

Commit

Permalink
Implemented browser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
netgusto committed Mar 27, 2014
1 parent a2d3480 commit d3748f7
Show file tree
Hide file tree
Showing 226 changed files with 812 additions and 2,471 deletions.
8 changes: 1 addition & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,4 @@ build/Release
# Dependency directory
# Deployed apps should consider commenting this line out:
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules

lib/*
!lib/.gitkeep

test/lib/*
!test/lib/.gitkeep
node_modules
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src
build.sh
2 changes: 2 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env sh
coffee -o lib/ -c src/upndown.coffee
716 changes: 716 additions & 0 deletions lib/upndown.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/upndown.min.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "upndown",
"description": "Markdown to HTML converter, written in Javascript",
"description": "Markdown to HTML converter",
"version": "0.0.1",
"keywords": [
"markdown",
"html",
"html2markdown"
"html2markdown",
"htmltomarkdown",
],
"maintainers": [
{
Expand Down
91 changes: 7 additions & 84 deletions src/upndown.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ do (
@currentollistack
@inlineelements
@tabindent
@debug
@textnodetype
@documentnodetype
@document
Expand All @@ -25,7 +24,6 @@ do (
@currentollistack = []
@inlineelements = ['_text', 'strong', 'b', 'i', 'em', 'u', 'a', 'img', 'code'] # code is considered inline, as it's converted as backticks in markdown
@tabindent = ' '
@debug = false
@textnodetype = 3
@documentnodetype = 9

Expand Down Expand Up @@ -69,10 +67,7 @@ do (
@methods = {}
@methods['_text'] = (node) =>

text = node.data
console.log('TAG:TEXT', "'" + text + "'") if @debug

text = @unescape(text)
text = @unescape(node.data)

if !(@hasParentOfType(node, 'code') && @isFirstChild(node)) && (!@isPreviousSiblingInline(node) || (@isFirstChild(node) && @hasParentOfType(node, 'li')))

Expand All @@ -88,20 +83,13 @@ do (
prefix = ''

@buffer[@currentdepth()].push(@escapeTextForMarkdown(node, text))
console.log('TEMPORARY', @buffer) if @debug

#if text.match(/\s\s$/) && !text.match(/^\s+$/)
# @buffer[@currentdepth()].push('<br/>')

@methods['open'] = {}
@methods['open']['_html'] = (node) =>
if(@isWrappingRootNode(node))
return

tag = node.tagName.toLowerCase()
console.log('OPEN:HTML', tag) if @debug
console.dir(node) if @debug

htmltag = '<' + tag

i = 0
Expand All @@ -120,100 +108,77 @@ do (


@methods['open']['p'] = (node) =>
console.log('OPEN:P') if @debug
@standardmethods.open._default(node, '')

@methods['open']['br'] = (node) =>
console.log('OPEN:BR') if @debug
@standardmethods.open._default(node, '')

@methods['open']['blockquote'] = (node) =>
console.log('OPEN:BLOCKQUOTE') if @debug
@standardmethods.open._default(node, '> ')

@methods['open']['pre'] = (node) =>
console.log('OPEN:PRE') if @debug
@standardmethods.open._default(node, @tabindent)

@methods['open']['code'] = (node) =>
console.log('OPEN:CODE') if @debug
@standardmethods.open._default(node, '')

@methods['open']['hr'] = (node) =>
console.log('OPEN:HR') if @debug
@standardmethods.open._default(node, '')

@methods['open']['ul'] = (node) =>
console.log('OPEN:UL') if @debug
@standardmethods.open._default(node, '')

@methods['open']['ol'] = (node) =>
console.log('OPEN:OL') if @debug
@currentollistack.push(1)
@standardmethods.open._default(node, '')

@methods['open']['li'] = (node) =>
console.log('OPEN:LI') if @debug
@standardmethods.open._default(node, @tabindent)

@methods['open']['h1'] = (node) =>
console.log('OPEN:H1') if @debug
@standardmethods.open._default(node, '')

@methods['open']['h2'] = (node) =>
console.log('OPEN:H2') if @debug
@standardmethods.open._default(node, '')

@methods['open']['h3'] = (node) =>
console.log('OPEN:H3') if @debug
@standardmethods.open._default(node, '')

@methods['open']['h4'] = (node) =>
console.log('OPEN:H4') if @debug
@standardmethods.open._default(node, '')

@methods['open']['h5'] = (node) =>
console.log('OPEN:H5') if @debug
@standardmethods.open._default(node, '')

@methods['open']['h6'] = (node) =>
console.log('OPEN:H6') if @debug
@standardmethods.open._default(node, '')

@methods['open']['strong'] = (node) =>
console.log('OPEN:STRONG') if @debug
@standardmethods.open._default(node, '')

@methods['open']['b'] = (node) =>
console.log('OPEN:B') if @debug
@standardmethods.open._default(node, '')

@methods['open']['em'] = (node) =>
console.log('OPEN:EM') if @debug
@standardmethods.open._default(node, '')

@methods['open']['i'] = (node) =>
console.log('OPEN:i') if @debug
@standardmethods.open._default(node, '')

@methods['open']['a'] = (node) =>
console.log('OPEN:A') if @debug
@standardmethods.open._default(node, '')

@methods['open']['img'] = (node) =>
console.log('OPEN:IMG') if @debug
@standardmethods.open._default(node, '')

@methods['open']['br'] = (node) =>
console.log('OPEN:BR') if @debug
@standardmethods.open._default(node, '')

@methods['close'] = {}

@methods['close']['_html'] = (node) =>
if(@isWrappingRootNode(node))
return
console.log('CLOSE:HTML', node.tagName.toLowerCase()) if @debug

depth = @currentdepth()
@depth--
Expand All @@ -224,8 +189,6 @@ do (
@buffer[depth-1].push(html + '</' + node.tagName.toLowerCase() + '>')

@methods['close']['p'] = (node) =>
console.log('CLOSE:P') if @debug

depth = @currentdepth()
@depth--
prefix = @prefixstack_pop()
Expand All @@ -240,8 +203,6 @@ do (
@buffer[depth-1].push(nl + html)

@methods['close']['hr'] = (node) =>
console.log('CLOSE:HR') if @debug

depth = @currentdepth()
@depth--
prefix = @prefixstack_pop()
Expand All @@ -255,8 +216,6 @@ do (
@buffer[depth-1].push(html)

@methods['close']['blockquote'] = (node) =>
console.log('CLOSE:BLOCKQUOTE') if @debug

depth = @currentdepth()
@depth--
prefix = @prefixstack_pop()
Expand All @@ -272,8 +231,6 @@ do (
@buffer[depth-1].push(nl + html)

@methods['close']['pre'] = (node) =>
console.log('CLOSE:PRE') if @debug

depth = @currentdepth()
@depth--
prefix = @prefixstack_pop()
Expand All @@ -298,8 +255,6 @@ do (
@buffer[depth-1].push(superextrapadding + beforenl + html)

@methods['close']['code'] = (node) =>
console.log('CLOSE:CODE') if @debug

depth = @currentdepth()
@depth--
prefix = @prefixstack_pop()
Expand All @@ -316,8 +271,6 @@ do (
@buffer[depth-1].push(html)

@methods['close']['ul'] = (node) =>
console.log('CLOSE:UL') if @debug

depth = @currentdepth()
@depth--
prefix = @prefixstack_pop()
Expand All @@ -335,13 +288,10 @@ do (
@buffer[depth-1].push(html)

@methods['close']['ol'] = (node) =>
console.log('CLOSE:OL') if @debug
@currentollistack.pop()
@methods.close.ul(node)

@methods['close']['li'] = (node) =>
console.log('CLOSE:LI') if @debug

depth = @currentdepth()
@depth--
prefix = @prefixstack_pop()
Expand All @@ -367,32 +317,24 @@ do (
@buffer[depth-1].push(html)

@methods['close']['h1'] = (node) =>
console.log('CLOSE:H1') if @debug
@standardmethods.close.hx(node)

@methods['close']['h2'] = (node) =>
console.log('CLOSE:H2') if @debug
@standardmethods.close.hx(node)

@methods['close']['h3'] = (node) =>
console.log('CLOSE:H3') if @debug
@standardmethods.close.hx(node)

@methods['close']['h4'] = (node) =>
console.log('CLOSE:H4') if @debug
@standardmethods.close.hx(node)

@methods['close']['h5'] = (node) =>
console.log('CLOSE:H5') if @debug
@standardmethods.close.hx(node)

@methods['close']['h6'] = (node) =>
console.log('CLOSE:H6') if @debug
@standardmethods.close.hx(node)

@methods['close']['strong'] = (node) =>
console.log('CLOSE:STRONG') if @debug

depth = @currentdepth()
@depth--
prefix = @prefixstack_pop()
Expand All @@ -402,12 +344,9 @@ do (
@buffer[depth-1].push(html + '**')

@methods['close']['b'] = (node) =>
console.log('CLOSE:B') if @debug
@methods.close.strong(node)

@methods['close']['em'] = (node) =>
console.log('CLOSE:EM') if @debug

depth = @currentdepth()
@depth--
prefix = @prefixstack_pop()
Expand All @@ -417,12 +356,9 @@ do (
@buffer[depth-1].push(html)

@methods['close']['i'] = (node) =>
console.log('CLOSE:I') if @debug
@methods.close.em(node)

@methods['close']['a'] = (node) =>
console.log('CLOSE:A') if @debug

depth = @currentdepth()
@depth--
prefix = @prefixstack_pop()
Expand All @@ -442,8 +378,6 @@ do (
@buffer[depth-1].push('[' + label + '](' + (if url then @unescape(url) else '') + (if title then (' "' + @unescape(title) + '"') else '') + ')')

@methods['close']['img'] = (node) =>
console.log('CLOSE:IMG') if @debug

depth = @currentdepth()
@depth--
prefix = @prefixstack_pop()
Expand All @@ -456,8 +390,6 @@ do (
@buffer[depth-1].push(html)

@methods['close']['br'] = (node) =>
console.log('CLOSE:BR') if @debug

depth = @currentdepth()
@depth--
prefix = @prefixstack_pop()
Expand All @@ -480,11 +412,9 @@ do (
''

prefixstack_push: (prefix) =>
console.log('PREFIX:PUSH:', @currentdepth(), "'" + prefix + "'") if @debug
@prefixstack[@currentdepth()] = prefix

prefixstack_pop: (prefix) =>
console.log('PREFIX:POP:', @currentdepth()) if @debug
before = @prefixstack[@currentdepth()]
@prefixstack[@currentdepth()] = ''
before
Expand Down Expand Up @@ -564,40 +494,29 @@ do (
dom.getElementById("hello")
, (node) =>
# text
console.log "'" + node.data + "'" if @debug
@methods['_text'](node)

, (node) =>
# open
console.log "open:" + node.tagName.toLowerCase() if @debug

if @methods.open[node.tagName.toLowerCase()]
@methods.open[node.tagName.toLowerCase()](node)
else
@methods.open['_html'](node)

, (node) =>
# explore
console.log "explore:" + node.tagName.toLowerCase() if @debug
null
, (node) =>
# close
console.log "close:" + node.tagName.toLowerCase() if @debug

if @methods.close[node.tagName.toLowerCase()]
@methods.close[node.tagName.toLowerCase()](node)
else
@methods.close['_html'](node)

)

#console.dir(@buffer[0].join('').split('\n'))
@buffer[0]
.join('')
#.replace(/^\s*/, '')
.replace(/\s*$/, '')
#.split('\n').slice(1,-1).join('\n') # get rid of wrapping div
#.replace(/^\s*/, '')
#.replace(/\s*$/, '')
.replace(/^[ \t]+$/gm, '')
.replace(/\n{3,}/gm, '\n\n\n') # 3 max for extra-padded pre next to li

Expand Down Expand Up @@ -710,7 +629,11 @@ do (
return @previoussiblingnontext(node.lastChild)

unescape: (html) =>
e = @document.createElement('div')
if @document
e = @document.createElement('div')
else
e = document.createElement('div')

e.innerHTML = html
if e.childNodes.length == 0
return ''
Expand Down
Loading

0 comments on commit d3748f7

Please sign in to comment.