Skip to content

Commit

Permalink
Closes #169. Add YAML Front Matter support to Markdown beautification
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavin001 committed Jan 2, 2015
1 parent 20206a6 commit e7a9800
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 74 deletions.
4 changes: 4 additions & 0 deletions examples/simple-jsbeautifyrc/markdown/expected/test.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
{}
---

- item
- item
- item
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
---
title: This is a title!
title: "This is a title!"
name: Derek Worthen
age: young
contact: null
email: "[email protected]"
address: some location
pets:
- cat
- dog
- bat
match: !<tag:yaml.org,2002:js/regexp> /pattern/gmi
run: !<tag:yaml.org,2002:js/function> "function () {\n \n \n \n\n\n}"
---

stuff
- item
- item
- item

1. one
2. two
3. three

more stuff

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---

title: This is a title!

name: Derek Worthen
age: young
contact:
email: [email protected]
address: some location
pets:
- cat
- dog
- bat
match: !!js/regexp /pattern/gim
run: !!js/function function() { }



---

- item
- item
- item

1. one
2. two
2. three
51 changes: 27 additions & 24 deletions lib/beautify.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -81,30 +81,33 @@ beautify = ({onSave}) ->
# Do nothing, is undefined
else if text instanceof Error
showError(text)
else if oldText isnt text
# console.log "Replacing current editor's text with new text"
posArray = getCursors(editor)
# console.log "posArray: #{posArray}"
origScrollTop = editor.getScrollTop()
# console.log "origScrollTop: #{origScrollTop}"
if not forceEntireFile and isSelection
selectedBufferRange = editor.getSelectedBufferRange()
# console.log "selectedBufferRange: #{selectedBufferRange}"
editor.setTextInBufferRange selectedBufferRange, text
else
# console.log "setText"
editor.setText text
# console.log "setCursors"
setCursors editor, posArray
# console.log "Done setCursors"
# Let the scrollTop setting run after all the save related stuff is run,
# otherwise setScrollTop is not working, probably because the cursor
# addition happens asynchronously
setTimeout (->
# console.log "setScrollTop"
editor.setScrollTop origScrollTop
return
), 0
else if typeof text is "string"
if oldText isnt text
# console.log "Replacing current editor's text with new text"
posArray = getCursors(editor)
# console.log "posArray: #{posArray}"
origScrollTop = editor.getScrollTop()
# console.log "origScrollTop: #{origScrollTop}"
if not forceEntireFile and isSelection
selectedBufferRange = editor.getSelectedBufferRange()
# console.log "selectedBufferRange: #{selectedBufferRange}"
editor.setTextInBufferRange selectedBufferRange, text
else
# console.log "setText"
editor.setText text
# console.log "setCursors"
setCursors editor, posArray
# console.log "Done setCursors"
# Let the scrollTop setting run after all the save related stuff is run,
# otherwise setScrollTop is not working, probably because the cursor
# addition happens asynchronously
setTimeout (->
# console.log "setScrollTop"
editor.setScrollTop origScrollTop
return
), 0
else
@showError(new Error("Unsupported beautification result '#{text}'."))
# else
# console.log "Already Beautiful!"
@loadingView.hide()
Expand Down
11 changes: 9 additions & 2 deletions lib/langs/cli-beautify.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ module.exports = (getCmd, isStdout) ->
temp.cleanup()
# Delete the output path
fs.unlink outputPath, (err) ->
console.log "Deleting output file", err if err
# console.log "Deleting output file", err if err
return
return

# Process the command
processCmd = (cmd) ->
processCmd = (cmd, optCallback) ->
if optCallback? and typeof optCallback is "function"
# console.log('Optional Callback found')
cb = callback # Save callback for later
callback = (output) -> # Wrap callback (cb) with optCallback
# console.log('Callback called!', output)
optCallback(output, cb)

if typeof cmd is "string"

config = env: process.env
Expand Down
64 changes: 57 additions & 7 deletions lib/langs/markdown-beautify.coffee
Original file line number Diff line number Diff line change
@@ -1,18 +1,68 @@
###
Requires http: //johnmacfarlane.net/pandoc/
###
getCmd = (inputPath, outputPath, options) ->
optionsStr = " --read markdown --write markdown --output \"" + outputPath + "\" \"" + inputPath + "\""
yamlFront = null
fs = null
yaml = null
allowUnsafeNewFunction = null

getCmd = (inputPath, outputPath, options, cb) ->
optionsStr = " --read markdown --write markdown --output \"" + outputPath + "\" \"" + inputPath + "\""
pandocPath = options.markdown_beautifier_path # jshint ignore: line
if pandocPath

yamlFrontMatter = options.yaml_front_matter # jshint ignore: line
cmd = ""
if pandocPath?
# Use absolute path
pandocPath + optionsStr
cmd = pandocPath + optionsStr
else

# Use command available in $PATH
"pandoc" + optionsStr
cmd = "pandoc" + optionsStr

if yamlFrontMatter?
# console.log("YAML Front Matter!")
fs ?= require "fs"
fs.readFile(inputPath, (err, contents) ->
# console.log('readFile', err, contents)
return cb(err) if err

# Parse with YAML front Matter
yamlFront ?= require "yaml-front-matter"
# console.log('Parse YAML Front Matter')
allowUnsafeNewFunction ?= require("loophole").allowUnsafeNewFunction
results = null
try
allowUnsafeNewFunction ->
results = yamlFront.loadFront(contents)
catch e
return cb(e)
newContents = results.__content # jshint ignore: line
delete results.__content # jshint ignore: line
# console.log('newContents', newContents)
# Write out new contents to input file
fs.writeFile(inputPath, newContents, (err) ->
# console.log('writeFile', err)
return cb(err) if err

# Completetion callback to combine YAML Front Matter and Markdown
completionCallback = (output, callback) ->
# console.log('Completion callback!')
try
yaml ?= require "js-yaml"
# Pre-pend YAML Front Matter to top of Markdown output
front = yaml.dump(results)
output = "---\n#{front}---\n\n#{output}"
# console.log('final output!', output)
return callback(output)
catch e
return callback(e)
# Run it all together now!
# console.log('Run!')
return cb(cmd, completionCallback)
)
)
return # Use Callback
else
return cmd # Return cmd synchronously
"use strict"
cliBeautify = require("./cli-beautify")
module.exports = cliBeautify(getCmd)
1 change: 1 addition & 0 deletions lib/language-options.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ module.exports =

# Markdown
markdown_pandoc_path: ""
markdown_yaml_front_matter: true

# Perl
perl_perltidy_path: "perltidy"
Expand Down
63 changes: 32 additions & 31 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,38 @@
"url": "https://github.com/vadirn"
}
],
"engines": {
"atom": ">0.50.0"
},
"dependencies": {
"analytics-node": "^1.0.2",
"async": "^0.9.0",
"atom-message-panel": "^1.1.1",
"coffee-formatter": "^0.1.1",
"editorconfig": "^0.11.4",
"emissary": "^1.0.0",
"extend": "^1.2.1",
"js-beautify": "^1.5.4",
"js-yaml": "^3.0.2",
"lodash": "2.4.1",
"loophole": "^1.0.0",
"node-dir": "^0.1.6",
"node-uuid": "^1.4.1",
"prettydiff": "^1.6.13",
"space-pen": "^4.3.0",
"strip-json-comments": "^0.1.3",
"temp": "^0.8.0",
"typescript-formatter": "~0.1.4",
"yaml-front-matter": "^3.2.3"
},
"activationEvents": [
"beautify",
"beautify:beautify-editor",
"beautify:beautify-file",
"beautify:beautify-directory",
"core:save",
"core:save-as"
],
"keywords": [
"atom",
"beautify",
Expand Down Expand Up @@ -78,36 +110,5 @@
"d",
"erb",
"editorconfig"
],
"engines": {
"atom": ">0.50.0"
},
"dependencies": {
"analytics-node": "^1.0.2",
"async": "^0.9.0",
"atom-message-panel": "^1.1.1",
"coffee-formatter": "^0.1.1",
"editorconfig": "^0.11.4",
"emissary": "^1.0.0",
"extend": "^1.2.1",
"js-beautify": "^1.5.4",
"js-yaml": "^3.0.2",
"lodash": "2.4.1",
"loophole": "^1.0.0",
"node-dir": "^0.1.6",
"node-uuid": "^1.4.1",
"prettydiff": "^1.6.13",
"space-pen": "^4.3.0",
"strip-json-comments": "^0.1.3",
"temp": "^0.8.0",
"typescript-formatter": "~0.1.4"
},
"activationEvents": [
"beautify",
"beautify:beautify-editor",
"beautify:beautify-file",
"beautify:beautify-directory",
"core:save",
"core:save-as"
]
}

0 comments on commit e7a9800

Please sign in to comment.