-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
112 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
const simpleHTMLTokenizer = require('simple-html-tokenizer'); | ||
const querystring = require('querystring'); | ||
const generate = require('./generate-xml-from-tokens.js'); | ||
const parseUnit = require('parse-absolute-css-unit'); | ||
const {program} = require('commander'); | ||
console.log('11'); | ||
|
||
function process(source, resourceQuery) { | ||
let tokens = simpleHTMLTokenizer.tokenize(source); | ||
|
||
tokens = tokens.map((tag) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison | ||
if (tag.type === 'StartTag' && tag.tagName === 'svg') { | ||
let width = undefined; | ||
let height = undefined; | ||
let viewBox = undefined; | ||
for (const attribute of tag.attributes) { | ||
if (attribute[0] === 'width') { | ||
try { | ||
width = parseUnit(attribute[1]); | ||
} catch (e) { | ||
console.warn('Unable to read width: ' + attribute[1]); | ||
} | ||
} | ||
if (attribute[0] === 'height') { | ||
try { | ||
height = parseUnit(attribute[1]); | ||
} catch (e) { | ||
console.warn('Unable to read height: ' + attribute[1]); | ||
} | ||
} | ||
if (attribute[0] === 'viewBox') { | ||
viewBox = attribute[1]; | ||
} | ||
} | ||
if (viewBox !== undefined && width === undefined && height === undefined) { | ||
try { | ||
const attrs = viewBox.split(' '); | ||
width = parseFloat(attrs[2]); | ||
height = parseFloat(attrs[3]); | ||
} catch (e) { | ||
console.warn('Unable to read viewBox: ' + viewBox); | ||
} | ||
} | ||
if (width !== undefined && height != undefined) { | ||
tag.attributes = tag.attributes.filter((attribute) => { | ||
return ( | ||
attribute[0] !== 'width' && | ||
attribute[0] !== 'height' && | ||
attribute[0] !== 'x' && | ||
attribute[0] !== 'y' | ||
); | ||
}); | ||
if (viewBox === undefined) { | ||
tag.attributes.push(['viewBox', `0 0 ${width} ${height}`, true]); | ||
} | ||
const queryString = resourceQuery.startsWith('?') ? resourceQuery.substring(1) : resourceQuery; | ||
const query = querystring.decode(queryString); | ||
if (query.width !== undefined) { | ||
const parsed = query.width.match(/^([0-9]+)([a-z]*)$/); | ||
tag.attributes.push(['width', query.width, true]); | ||
tag.attributes.push(['height', `${(height / width) * parseFloat(parsed[1])}${parsed[2]}`, true]); | ||
} else if (query.height !== undefined) { | ||
const parsed = query.height.match(/^([0-9]+)([a-z]*)$/); | ||
tag.attributes.push(['width', `${(width / height) * parseFloat(parsed[1])}${parsed[2]}`, true]); | ||
tag.attributes.push(['height', query.height, true]); | ||
} else { | ||
tag.attributes.push(['width', width, true]); | ||
tag.attributes.push(['height', height, true]); | ||
} | ||
} | ||
} | ||
return tag; | ||
}); | ||
|
||
return generate(tokens); | ||
} | ||
|
||
function main(args) { | ||
uri = args[0]; | ||
resourceQuery = args[1]; | ||
const fs = require('fs'); | ||
const source = fs.readFileSync(uri, 'utf-8'); | ||
const result = process(source, resourceQuery); | ||
fs.writeFileSync(uri, result, 'utf-8'); | ||
} | ||
|
||
// If running this module directly then call the main function. | ||
if (require.main === module) { | ||
program.parse(process.argv); | ||
main(program.args); | ||
} | ||
|
||
module.exports = main; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters