Implemented options and a clean layout
This commit is contained in:
parent
a4ff384dbf
commit
ff377a7a3a
|
@ -1,6 +1,9 @@
|
|||
"use strict"
|
||||
Beautifier = require('./beautifier')
|
||||
path = require('path')
|
||||
fs = require("fs")
|
||||
temp = require("temp").track()
|
||||
|
||||
|
||||
module.exports = class LatexBeautify extends Beautifier
|
||||
name: "Latex Beautify"
|
||||
|
@ -9,19 +12,61 @@ module.exports = class LatexBeautify extends Beautifier
|
|||
LaTeX: true
|
||||
}
|
||||
|
||||
# There are too many options with latexmk, I have tried to slim this down to the most useful ones.
|
||||
# This method creates a configuration file for latexindent.
|
||||
buildConfigFile: (options) ->
|
||||
indentChar = options.indent_char
|
||||
if options.indent_with_tabs
|
||||
indentChar = "\t"
|
||||
# +true = 1 and +false = 0
|
||||
config = """
|
||||
defaultIndent: \"#{indentChar}\"
|
||||
alwaysLookforSplitBraces: #{+options.always_look_for_split_braces}
|
||||
alwaysLookforSplitBrackets: #{+options.always_look_for_split_brackets}
|
||||
indentPreamble: #{+options.indent_preamble}
|
||||
removeTrailingWhitespace: #{+options.remove_trailing_whitespace}
|
||||
lookForAlignDelims:\n
|
||||
"""
|
||||
for delim in options.align_columns_in_environments
|
||||
config += "\t#{delim}: 1\n"
|
||||
return config
|
||||
|
||||
# Latexindent accepts configuration _files_ only.
|
||||
# This file has to be named localSettings.yaml and be in the same folder as the tex file.
|
||||
# It also insists on creating a log file somewhere.
|
||||
# So we set up a directory with all the files in place.
|
||||
setUpDir: (dirPath, text, config) ->
|
||||
@texFile = path.join(dirPath, "latex.tex")
|
||||
fs.writeFile @texFile, text, (err) ->
|
||||
return reject(err) if err
|
||||
@configFile = path.join(dirPath, "localSettings.yaml")
|
||||
fs.writeFile @configFile, config, (err) ->
|
||||
return reject(err) if err
|
||||
@logFile = path.join(dirPath, "indent.log")
|
||||
fs.writeFile @logFile, "", (err) ->
|
||||
return reject(err) if err
|
||||
|
||||
#Beautifier does not currently have a method for creating directories, so we call temp directly.
|
||||
beautify: (text, language, options) ->
|
||||
@tempFile("indent", "", "log")
|
||||
.then( (logFile)=>
|
||||
logDir = path.dirname logFile
|
||||
@tempTex = @tempFile("latex", text)
|
||||
new @Promise((resolve, reject) ->
|
||||
temp.mkdir("latex", (err, dirPath) ->
|
||||
return reject(err) if err
|
||||
resolve(dirPath)
|
||||
)
|
||||
)
|
||||
.then((dirPath)=>
|
||||
@setUpDir(dirPath, text, @buildConfigFile(options))
|
||||
run = @run "latexindent", [
|
||||
"-o"
|
||||
"-s"
|
||||
"-c=" + logDir
|
||||
@tempTex
|
||||
@tempTex
|
||||
]
|
||||
"-o" #Output to the same location as file, -w creates a backup file, whereas this does not
|
||||
"-s" #Silent mode
|
||||
"-l" #Tell latexindent we have a local configuration file
|
||||
"-c=" + dirPath #Tell latexindent to place the log file in this directory
|
||||
@texFile
|
||||
@texFile
|
||||
], help: {
|
||||
link: "https://github.com/cmhughes/latexindent.pl"
|
||||
}
|
||||
)
|
||||
.then( =>
|
||||
@readFile(@tempTex)
|
||||
)
|
||||
@readFile(@texFile)
|
||||
)
|
||||
|
|
|
@ -31,85 +31,32 @@ module.exports = {
|
|||
|
||||
###
|
||||
options:
|
||||
# JavaScript
|
||||
indent_size:
|
||||
type: 'integer'
|
||||
default: defaultIndentSize
|
||||
minimum: 0
|
||||
description: "Indentation size/length"
|
||||
indent_char:
|
||||
type: 'string'
|
||||
default: defaultIndentChar
|
||||
description: "Indentation character"
|
||||
indent_level:
|
||||
type: 'integer'
|
||||
default: 0
|
||||
description: "Initial indentation level"
|
||||
indent_with_tabs:
|
||||
type: 'boolean'
|
||||
default: defaultIndentWithTabs
|
||||
description: "Indentation uses tabs, overrides `Indent Size` and `Indent Char`"
|
||||
preserve_newlines:
|
||||
indent_preamble:
|
||||
type: 'boolean'
|
||||
default: false
|
||||
description: "Indent the preable"
|
||||
always_look_for_split_braces:
|
||||
type: 'boolean'
|
||||
default: true
|
||||
description: "Preserve line-breaks"
|
||||
max_preserve_newlines:
|
||||
type: 'integer'
|
||||
default: 10
|
||||
description: "Number of line-breaks to be preserved in one chunk"
|
||||
space_in_paren:
|
||||
description: "If `latexindent` should look for commands that split braces across lines"
|
||||
always_look_for_split_brackets:
|
||||
type: 'boolean'
|
||||
default: false
|
||||
description: "Add padding spaces within paren, ie. f( a, b )"
|
||||
jslint_happy:
|
||||
description: "If `latexindent` should look for commands that split brackets across lines"
|
||||
remove_trailing_whitespace:
|
||||
type: 'boolean'
|
||||
default: false
|
||||
description: "Enable jslint-stricter mode"
|
||||
space_after_anon_function:
|
||||
type: 'boolean'
|
||||
default: false
|
||||
description: "Add a space before an anonymous function's parens, ie. function ()"
|
||||
brace_style:
|
||||
type: 'string'
|
||||
default: "collapse"
|
||||
enum: ["collapse", "collapse-preserve-inline", "expand", "end-expand", "none"]
|
||||
description: "[collapse|collapse-preserve-inline|expand|end-expand|none]"
|
||||
break_chained_methods:
|
||||
type: 'boolean'
|
||||
default: false
|
||||
description: "Break chained method calls across subsequent lines"
|
||||
keep_array_indentation:
|
||||
type: 'boolean'
|
||||
default: false
|
||||
description: "Preserve array indentation"
|
||||
keep_function_indentation:
|
||||
type: 'boolean'
|
||||
default: false
|
||||
description: ""
|
||||
space_before_conditional:
|
||||
type: 'boolean'
|
||||
default: true
|
||||
description: ""
|
||||
eval_code:
|
||||
type: 'boolean'
|
||||
default: false
|
||||
description: ""
|
||||
unescape_strings:
|
||||
type: 'boolean'
|
||||
default: false
|
||||
description: "Decode printable characters encoded in xNN notation"
|
||||
wrap_line_length:
|
||||
type: 'integer'
|
||||
default: 0
|
||||
description: "Wrap lines at next opportunity after N characters"
|
||||
end_with_newline:
|
||||
type: 'boolean'
|
||||
default: false
|
||||
description: "End output with newline"
|
||||
end_with_comma:
|
||||
type: 'boolean'
|
||||
default: false
|
||||
description: "If a terminating comma should be inserted into \
|
||||
arrays, object literals, and destructured objects."
|
||||
|
||||
description: "Remove trailing whitespace"
|
||||
align_columns_in_environments:
|
||||
type: 'array'
|
||||
default:["tabular", "matrix", "bmatrix", "pmatrix"]
|
||||
decription: "Aligns columns by the alignment tabs for environments specified"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue