mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-19 23:39:50 +02:00
Auto fix standard style voilations
This commit is contained in:
@@ -6,17 +6,17 @@
|
||||
* https://github.com/filamentgroup/grunticon
|
||||
*/
|
||||
|
||||
phantom.onError = function(msg, trace) {
|
||||
phantom.onError = function (msg, trace) {
|
||||
var msgStack = ['PHANTOM ERROR: ' + msg]
|
||||
if (trace && trace.length) {
|
||||
msgStack.push('TRACE:')
|
||||
trace.forEach(function(t) {
|
||||
trace.forEach(function (t) {
|
||||
msgStack.push(
|
||||
' -> '
|
||||
+ (t.file || t.sourceURL)
|
||||
+ ': '
|
||||
+ t.line
|
||||
+ (t.function ? ' (in function ' + t.function +')' : '')
|
||||
' -> ' +
|
||||
(t.file || t.sourceURL) +
|
||||
': ' +
|
||||
t.line +
|
||||
(t.function ? ' (in function ' + t.function + ')' : '')
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -24,58 +24,57 @@ phantom.onError = function(msg, trace) {
|
||||
phantom.exit(1)
|
||||
}
|
||||
|
||||
var system = require('system')
|
||||
, fs = require('fs')
|
||||
, webpage = require('webpage')
|
||||
var system = require('system'),
|
||||
fs = require('fs'),
|
||||
webpage = require('webpage')
|
||||
|
||||
var page = webpage.create()
|
||||
, files = system.args.slice(10, system.args.length)
|
||||
, width = system.args[8]
|
||||
var page = webpage.create(),
|
||||
files = system.args.slice(10, system.args.length),
|
||||
width = system.args[8]
|
||||
|
||||
if(typeof width === 'undefined' || width==='undefined'){
|
||||
width = 1200;
|
||||
if (typeof width === 'undefined' || width === 'undefined') {
|
||||
width = 1200
|
||||
}
|
||||
var options = {
|
||||
outputDir: system.args[1]
|
||||
, png: system.args[2] === 'true' ? true : false
|
||||
, svg: system.args[3] === 'true' ? true : false
|
||||
, css: fs.read(system.args[4])
|
||||
, sequenceConfig: system.args[5] !== 'null' ? JSON.parse(fs.read(system.args[5])) : {}
|
||||
, ganttConfig: system.args[6] !== 'null' ? JSON.parse(fs.read(system.args[6])) : {}
|
||||
, verbose: system.args[7] === 'true' ? true : false
|
||||
, width: width
|
||||
, outputSuffix: system.args[9]
|
||||
}
|
||||
, log = logger(options.verbose)
|
||||
options.sequenceConfig.useMaxWidth = false;
|
||||
outputDir: system.args[1],
|
||||
png: system.args[2] === 'true',
|
||||
svg: system.args[3] === 'true',
|
||||
css: fs.read(system.args[4]),
|
||||
sequenceConfig: system.args[5] !== 'null' ? JSON.parse(fs.read(system.args[5])) : {},
|
||||
ganttConfig: system.args[6] !== 'null' ? JSON.parse(fs.read(system.args[6])) : {},
|
||||
verbose: system.args[7] === 'true',
|
||||
width: width,
|
||||
outputSuffix: system.args[9]
|
||||
},
|
||||
log = logger(options.verbose)
|
||||
options.sequenceConfig.useMaxWidth = false
|
||||
|
||||
page.content = [
|
||||
'<html>'
|
||||
, '<head>'
|
||||
, '<style type="text/css">body {background:white;font-family: Arial;}'
|
||||
, options.css
|
||||
, '</style>'
|
||||
, '</head>'
|
||||
, '<body>'
|
||||
, '</body>'
|
||||
, '</html>'
|
||||
'<html>',
|
||||
'<head>',
|
||||
'<style type="text/css">body {background:white;font-family: Arial;}',
|
||||
options.css,
|
||||
'</style>',
|
||||
'</head>',
|
||||
'<body>',
|
||||
'</body>',
|
||||
'</html>'
|
||||
].join('\n')
|
||||
|
||||
|
||||
page.injectJs('../dist/mermaid.js')
|
||||
page.onConsoleMessage = function(msg, lineNum, sourceId) {
|
||||
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
|
||||
};
|
||||
page.onConsoleMessage = function (msg, lineNum, sourceId) {
|
||||
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")')
|
||||
}
|
||||
|
||||
console.log('Num files to execute : ' + files.length)
|
||||
|
||||
files.forEach(function(file) {
|
||||
var contents = fs.read(file)
|
||||
, filename = file.split(fs.separator).slice(-1)
|
||||
, oParser = new DOMParser()
|
||||
, oDOM
|
||||
, svgContent
|
||||
, allElements;
|
||||
files.forEach(function (file) {
|
||||
var contents = fs.read(file),
|
||||
filename = file.split(fs.separator).slice(-1),
|
||||
oParser = new DOMParser(),
|
||||
oDOM,
|
||||
svgContent,
|
||||
allElements
|
||||
|
||||
console.log('ready to execute png: ' + filename + '.png ')
|
||||
|
||||
@@ -83,14 +82,14 @@ files.forEach(function(file) {
|
||||
// look like it. we need to serialize then unserialize the svgContent that's
|
||||
// taken from the DOM
|
||||
svgContent = page.evaluate(executeInPage, {
|
||||
contents : contents,
|
||||
ganttConfig : options.ganttConfig,
|
||||
sequenceConfig : options.sequenceConfig,
|
||||
confWidth : options.width
|
||||
contents: contents,
|
||||
ganttConfig: options.ganttConfig,
|
||||
sequenceConfig: options.sequenceConfig,
|
||||
confWidth: options.width
|
||||
})
|
||||
|
||||
oDOM = oParser.parseFromString(svgContent, "text/xml")
|
||||
|
||||
oDOM = oParser.parseFromString(svgContent, 'text/xml')
|
||||
|
||||
resolveSVGElement(oDOM.firstChild)
|
||||
setSVGStyle(oDOM.firstChild, options.css)
|
||||
|
||||
@@ -100,22 +99,22 @@ files.forEach(function(file) {
|
||||
for (var i = 0, len = allElements.length; i < len; i++) {
|
||||
resolveForeignObjects(allElements[i])
|
||||
}
|
||||
|
||||
var outputPath=options.outputDir + fs.separator + filename + options.outputSuffix;
|
||||
|
||||
var outputPath = options.outputDir + fs.separator + filename + options.outputSuffix
|
||||
if (options.png) {
|
||||
page.viewportSize = {
|
||||
width: ~~oDOM.documentElement.attributes.getNamedItem('width').value
|
||||
, height: ~~oDOM.documentElement.attributes.getNamedItem('height').value
|
||||
width: ~~oDOM.documentElement.attributes.getNamedItem('width').value,
|
||||
height: ~~oDOM.documentElement.attributes.getNamedItem('height').value
|
||||
}
|
||||
|
||||
page.render(outputPath+'.png')
|
||||
page.render(outputPath + '.png')
|
||||
console.log('saved png: ' + filename + '.png')
|
||||
}
|
||||
|
||||
if (options.svg) {
|
||||
var serialize = new XMLSerializer();
|
||||
fs.write(outputPath+'.svg'
|
||||
, serialize.serializeToString(oDOM)+'\n'
|
||||
var serialize = new XMLSerializer()
|
||||
fs.write(outputPath + '.svg'
|
||||
, serialize.serializeToString(oDOM) + '\n'
|
||||
, 'w'
|
||||
)
|
||||
log('saved svg: ' + filename + '.svg')
|
||||
@@ -124,13 +123,13 @@ files.forEach(function(file) {
|
||||
|
||||
phantom.exit()
|
||||
|
||||
function logger(_verbose) {
|
||||
function logger (_verbose) {
|
||||
var verbose = _verbose
|
||||
|
||||
return function(_message, _level) {
|
||||
var level = level
|
||||
, message = _message
|
||||
, log
|
||||
return function (_message, _level) {
|
||||
var level = level,
|
||||
message = _message,
|
||||
log
|
||||
|
||||
log = level === 'error' ? system.stderr : system.stdout
|
||||
|
||||
@@ -140,17 +139,17 @@ function logger(_verbose) {
|
||||
}
|
||||
}
|
||||
|
||||
function traverse(obj){
|
||||
function traverse (obj) {
|
||||
var tree = []
|
||||
|
||||
tree.push(obj)
|
||||
visit(obj)
|
||||
|
||||
function visit(node) {
|
||||
function visit (node) {
|
||||
if (node && node.hasChildNodes()) {
|
||||
var child = node.firstChild
|
||||
while (child) {
|
||||
if (child.nodeType === 1 && child.nodeName != 'SCRIPT'){
|
||||
if (child.nodeType === 1 && child.nodeName != 'SCRIPT') {
|
||||
tree.push(child)
|
||||
visit(child)
|
||||
}
|
||||
@@ -162,41 +161,41 @@ function traverse(obj){
|
||||
return tree
|
||||
}
|
||||
|
||||
function resolveSVGElement(element) {
|
||||
function resolveSVGElement (element) {
|
||||
var prefix = {
|
||||
xmlns: "http://www.w3.org/2000/xmlns/"
|
||||
, xlink: "http://www.w3.org/1999/xlink"
|
||||
, svg: "http://www.w3.org/2000/svg"
|
||||
}
|
||||
, doctype = '<!DOCTYPE svg:svg PUBLIC'
|
||||
+ ' "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"'
|
||||
+ ' "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">'
|
||||
xmlns: 'http://www.w3.org/2000/xmlns/',
|
||||
xlink: 'http://www.w3.org/1999/xlink',
|
||||
svg: 'http://www.w3.org/2000/svg'
|
||||
},
|
||||
doctype = '<!DOCTYPE svg:svg PUBLIC' +
|
||||
' "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"' +
|
||||
' "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg.dtd">'
|
||||
|
||||
element.setAttribute("version", "1.1")
|
||||
element.setAttribute('version', '1.1')
|
||||
// removing attributes so they aren't doubled up
|
||||
element.removeAttribute("xmlns")
|
||||
element.removeAttribute("xlink")
|
||||
element.removeAttribute('xmlns')
|
||||
element.removeAttribute('xlink')
|
||||
// These are needed for the svg
|
||||
if (!element.hasAttributeNS(prefix.xmlns, "xmlns")) {
|
||||
element.setAttributeNS(prefix.xmlns, "xmlns", prefix.svg)
|
||||
if (!element.hasAttributeNS(prefix.xmlns, 'xmlns')) {
|
||||
element.setAttributeNS(prefix.xmlns, 'xmlns', prefix.svg)
|
||||
}
|
||||
if (!element.hasAttributeNS(prefix.xmlns, "xmlns:xlink")) {
|
||||
element.setAttributeNS(prefix.xmlns, "xmlns:xlink", prefix.xlink)
|
||||
if (!element.hasAttributeNS(prefix.xmlns, 'xmlns:xlink')) {
|
||||
element.setAttributeNS(prefix.xmlns, 'xmlns:xlink', prefix.xlink)
|
||||
}
|
||||
}
|
||||
|
||||
function setSVGStyle(svg, css) {
|
||||
if (!css || !svg) {return}
|
||||
var styles=svg.getElementsByTagName('style');
|
||||
if (!styles || styles.length==0) { return }
|
||||
styles[0].textContent = css;
|
||||
function setSVGStyle (svg, css) {
|
||||
if (!css || !svg) { return }
|
||||
var styles = svg.getElementsByTagName('style')
|
||||
if (!styles || styles.length == 0) { return }
|
||||
styles[0].textContent = css
|
||||
}
|
||||
|
||||
function resolveForeignObjects(element) {
|
||||
return;
|
||||
var children
|
||||
, textElement
|
||||
, textSpan
|
||||
function resolveForeignObjects (element) {
|
||||
return
|
||||
var children,
|
||||
textElement,
|
||||
textSpan
|
||||
|
||||
if (element.tagName === 'foreignObject') {
|
||||
textElement = document.createElement('text')
|
||||
@@ -216,21 +215,21 @@ function resolveForeignObjects(element) {
|
||||
}
|
||||
|
||||
// The sandboxed function that's executed in-page by phantom
|
||||
function executeInPage(data) {
|
||||
var xmlSerializer = new XMLSerializer()
|
||||
, contents = data.contents
|
||||
, sequenceConfig = JSON.stringify(data.sequenceConfig)
|
||||
, ganttConfig = JSON.stringify(data.ganttConfig).replace(/"(function.*})"/, "$1")
|
||||
, toRemove
|
||||
, el
|
||||
, elContent
|
||||
, svg
|
||||
, svgValue
|
||||
, boundingBox
|
||||
, width
|
||||
, height
|
||||
, confWidth = data.confWidth
|
||||
|
||||
function executeInPage (data) {
|
||||
var xmlSerializer = new XMLSerializer(),
|
||||
contents = data.contents,
|
||||
sequenceConfig = JSON.stringify(data.sequenceConfig),
|
||||
ganttConfig = JSON.stringify(data.ganttConfig).replace(/"(function.*})"/, '$1'),
|
||||
toRemove,
|
||||
el,
|
||||
elContent,
|
||||
svg,
|
||||
svgValue,
|
||||
boundingBox,
|
||||
width,
|
||||
height,
|
||||
confWidth = data.confWidth
|
||||
|
||||
var toRemove = document.getElementsByClassName('mermaid')
|
||||
if (toRemove && toRemove.length) {
|
||||
for (var i = 0, len = toRemove.length; i < len; i++) {
|
||||
@@ -238,43 +237,42 @@ function executeInPage(data) {
|
||||
}
|
||||
}
|
||||
|
||||
var el = document.createElement("div")
|
||||
var el = document.createElement('div')
|
||||
el.className = 'mermaid'
|
||||
el.appendChild(document.createTextNode(contents))
|
||||
document.body.appendChild(el)
|
||||
|
||||
|
||||
var config = {
|
||||
sequenceDiagram: JSON.parse(sequenceConfig),
|
||||
flowchart: {useMaxWidth: false},
|
||||
logLevel: 1
|
||||
};
|
||||
|
||||
mermaid.initialize(config);
|
||||
|
||||
var sc = document.createElement("script")
|
||||
}
|
||||
|
||||
mermaid.initialize(config)
|
||||
|
||||
var sc = document.createElement('script')
|
||||
sc.appendChild(document.createTextNode('mermaid.ganttConfig = ' + ganttConfig + ';'))
|
||||
document.body.appendChild(sc)
|
||||
|
||||
mermaid.init();
|
||||
mermaid.init()
|
||||
|
||||
svg = document.querySelector('svg')
|
||||
|
||||
boundingBox = svg.getBoundingClientRect(); // the initial bonding box of the svg
|
||||
width = boundingBox.width * 1.5; // adding the scale factor for consistency with output in chrome browser
|
||||
height = boundingBox.height * 1.5; // adding the scale factor for consistency with output in chrome browser
|
||||
boundingBox = svg.getBoundingClientRect() // the initial bonding box of the svg
|
||||
width = boundingBox.width * 1.5 // adding the scale factor for consistency with output in chrome browser
|
||||
height = boundingBox.height * 1.5 // adding the scale factor for consistency with output in chrome browser
|
||||
|
||||
|
||||
var scalefactor = confWidth/(width-8);
|
||||
var scalefactor = confWidth / (width - 8)
|
||||
|
||||
// resizing the body to fit the svg
|
||||
document.body.setAttribute(
|
||||
'style'
|
||||
, 'width: ' + (confWidth-8) + '; height: ' + (height*scalefactor) + ';'
|
||||
, 'width: ' + (confWidth - 8) + '; height: ' + (height * scalefactor) + ';'
|
||||
)
|
||||
// resizing the svg via css for consistent display
|
||||
svg.setAttribute(
|
||||
'style'
|
||||
, 'width: ' + (confWidth-8) + '; height: ' + (height*scalefactor) + ';'
|
||||
, 'width: ' + (confWidth - 8) + '; height: ' + (height * scalefactor) + ';'
|
||||
)
|
||||
|
||||
// set witdth and height attributes used to set the viewport when rending png image
|
||||
@@ -284,10 +282,10 @@ function executeInPage(data) {
|
||||
)
|
||||
svg.setAttribute(
|
||||
'height'
|
||||
, height*scalefactor
|
||||
, height * scalefactor
|
||||
)
|
||||
|
||||
svgValue = xmlSerializer.serializeToString(svg)+"\n";
|
||||
//console.log('confWidth: '+document.head.outerHTML);
|
||||
svgValue = xmlSerializer.serializeToString(svg) + '\n'
|
||||
// console.log('confWidth: '+document.head.outerHTML);
|
||||
return svgValue
|
||||
}
|
||||
|
Reference in New Issue
Block a user