Implement highlighting of Markdown pre blocks

This commit is contained in:
Garen Torikian
2013-04-29 16:43:46 -07:00
committed by Garen Torikian & Nathan Sobo
parent dd041945d2
commit cf239ccac0
3 changed files with 49 additions and 59 deletions

View File

@@ -3,8 +3,10 @@ _ = require 'underscore'
ScrollView = require 'scroll-view'
{$$$} = require 'space-pen'
roaster = require 'roaster'
LanguageMode = require 'language-mode'
Buffer = require 'text-buffer'
Editor = require 'editor'
fenceNameToExtension =
"ruby": "rb"
module.exports =
class MarkdownPreviewView extends ScrollView
@@ -63,27 +65,32 @@ class MarkdownPreviewView extends ScrollView
setLoading: ->
@html($$$ -> @div class: 'markdown-spinner', 'Loading Markdown...')
tokenizeCodeBlocks: (html) =>
html = $(html)
preList = $(html.filter("pre"))
for codeBlock in preList.toArray()
codeBlock = $(codeBlock.firstChild)
if className = codeBlock.attr('class')
fenceName = className.replace(/^lang-/, '')
if extension = fenceNameToExtension[fenceName]
text = codeBlock.text()
syntax.selectGrammar("foo.#{extension}", text)
if grammar = syntax.selectGrammar("foo.#{extension}", text)
continue if grammar is syntax.nullGrammar
tokens = grammar.tokenizeLines(text)
grouping = ""
for token in tokens
grouping += Editor.buildLineHtml(token, text)
codeBlock.replaceWith(grouping)
html
fetchRenderedMarkdown: ->
@setLoading()
roaster(@buffer.getText(), {}, (err, html) =>
if err
@setErrorHtml(err)
else
result = @html(html)
preList = result.find("pre")
for pre in preList
grammar = _.find syntax.grammars, (grammar) ->
return "ruby" == grammar.scopeName.split(".").pop()
if grammar
languageMode = new LanguageMode(this, grammar)
console.log pre
code = pre.childNodes[0]
text = code.textContent.split("\n")
for line in text
tokens = languageMode.tokenizeLine(text)
console.log tokens
#codeBuffer = new Buffer("", text)
#x = 1
@html(@tokenizeCodeBlocks(html))
)

View File

@@ -8,6 +8,7 @@ describe "MarkdownPreviewView", ->
beforeEach ->
project.setPath(project.resolve('markdown'))
buffer = project.bufferForPath('file.markdown')
atom.activatePackage('ruby.tmbundle', sync: true)
preview = new MarkdownPreviewView(buffer)
afterEach ->
@@ -35,7 +36,7 @@ describe "MarkdownPreviewView", ->
fdescribe "code block tokenization", ->
describe "when the code block's fence name has a matching grammar", ->
it "tokenizes the code block with the grammar", ->
expect(preview.find("pre code.lang-ruby .entity.name.function.ruby")).toExist()
expect(preview.find("pre span.entity.name.function.ruby")).toExist()
describe "when the code block's fence name doesn't have a matching grammar", ->
it "does not tokenize the code block", ->