Skip to content

Commit

Permalink
v0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
froquede committed Dec 9, 2024
1 parent 79f5d72 commit 53e1307
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 1,033 deletions.
5 changes: 1 addition & 4 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"assets": {
"VCR": {
"path": "url('project:/Assets/Fonts/VCR_OSD_MONO_1.001.ttf')"
}
},
"options": {
"uppercase": true
"uppercase": false
}
}
39 changes: 39 additions & 0 deletions convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env node

const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const path = require("path");

const argv = yargs(hideBin(process.argv))
.option('i', {
alias: 'input',
describe: 'Input HTML files',
demandOption: true,
type: 'array',
})
.option('css', {
describe: 'CSS files',
demandOption: true,
type: 'array',
})
.option('reset', {
describe: 'Reset CSS file',
default: path.resolve(__dirname, 'reset.css'),
type: 'string',
})
.option('c', {
alias: 'config',
describe: 'Configuration file (JSON)',
default: path.resolve(__dirname, 'config.json'),
type: 'string'
})
.option('o', {
alias: 'output',
describe: 'Output folder',
default: path.resolve(__dirname, 'results'),
type: 'string'
})
.help()
.argv;

require('./index.js')(argv);
86 changes: 60 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,25 @@ const css = require('css');
const uss_properties = require('./uss_properties.json');
const config = require('./config.json');

const html = [{name: 'panasonic_menu.html', data: fs.readFileSync('panasonic_menu.html', 'utf8')}];
const cssContent = [{name: 'reset.css', data: fs.readFileSync('reset.css', 'utf8')}, {name: 'panasonic_style.css', data: fs.readFileSync('panasonic_style.css', 'utf8')}]
let html, cssContent, outputFolder;

let xmlheader = '<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False">';
let xmlfooter = '</ui:UXML>';
let resetAll = 'body, div, p';

for(let i = 0; i < html.length; i++) {
html2uxml(html[i].name.split('.html').join(''), html[i].data);
}

for(let i = 0; i < cssContent.length; i++) {
convertCss(cssContent[i].name.split('.css').join(''), cssContent[i].data);
}

function html2uxml(name, h) {
const $ = cheerio.load(h);
let parsed = convertToXML($('body'), $);

parsed = parsed.split('<body>').join(xmlheader);
parsed = parsed.split('</body>').join(xmlfooter);


fs.writeFile('./results/' + name + '.uxml', parsed, 'utf-8', err => {
fs.writeFile('./results/' + name + '.uxml', formatXml(parsed), 'utf-8', err => {
if(err) console.log(err);
else console.log(name + ' UXML written ✓');
else {
console.log(name + ' UXML written ✓');
}
});
}

Expand Down Expand Up @@ -60,26 +54,21 @@ function convertToXML(element, $) {
}
});

if (element.children().length > 0) {
xmlString += '>';

element.children().each((index, child) => {
const childElement = $(child);
xmlString += convertToXML(childElement, $);
});
xmlString += '>';

xmlString += `</${tagName}>`;
} else {
xmlString += ' />';
}
element.children().each((index, child) => {
const childElement = $(child);
xmlString += convertToXML(childElement, $);
});

xmlString += `</${tagName}>`;

return xmlString;
}

function convertCss(name, data) {
let parsedCSS = css.parse(data);
fs.writeFile('./results/' + name + '.uss', css2uss(parsedCSS.stylesheet.rules), 'utf-8', err => {
fs.writeFile(`${outputFolder}/` + name + '.uss', css2uss(parsedCSS.stylesheet.rules), 'utf-8', err => {
if(err) console.log(err);
else console.log(name + ' USS written ✓');
});
Expand All @@ -97,7 +86,7 @@ function css2uss(rules) {
for(let d = 0; d < rule.declarations.length; d++) {
let declaration = rule.declarations[d];
let property = transformProperty(declaration.property);
console.log(property);
//console.log(property);
if (uss_properties[property]) {
if(uss_properties[property].native == true) {
let value = translateValue(declaration.value, property);
Expand Down Expand Up @@ -139,4 +128,49 @@ function getExtras(property, value) {
let extras = '';
extras += property == '-unity-font' ? ' -unity-font-definition: none;\n' : '';
return extras;
}
}

function convert(argv) {
console.log(argv);

outputFolder = argv.output;

html = [];
for (let i = 0; i < argv.input.length; i++) {
let path = argv.input[i];
html.push({
name: path,
data: fs.readFileSync(path, 'utf8')
})
}

cssContent = [];
for (let i = 0; i < argv.css.length; i++) {
let path = argv.css[i];
cssContent.push({
name: path,
data: fs.readFileSync(path, 'utf8')
})
}

for(let i = 0; i < html.length; i++) {
html2uxml(html[i].name.split('.html').join(''), html[i].data);
}

for(let i = 0; i < cssContent.length; i++) {
convertCss(cssContent[i].name.split('.css').join(''), cssContent[i].data);
}
};

function formatXml(xml, tab) { // tab = optional indent value, default is tab (\t)
var formatted = '', indent= '';
tab = tab || '\t';
xml.split(/>\s*</).forEach(function(node) {
if (node.match( /^\/\w/ )) indent = indent.substring(tab.length); // decrease indent by one 'tab'
formatted += indent + '<' + node + '>\r\n';
if (node.match( /^<?\w[^>]*[^\/]$/ )) indent += tab; // increase indent
});
return formatted.substring(1, formatted.length-3);
}

module.exports = convert;
Loading

0 comments on commit 53e1307

Please sign in to comment.