Skip to content

Instantly share code, notes, and snippets.

@BeFiveINFO
Last active November 16, 2019 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BeFiveINFO/1259974a77b5edd0bc368220a4b5ef1a to your computer and use it in GitHub Desktop.
Save BeFiveINFO/1259974a77b5edd0bc368220a4b5ef1a to your computer and use it in GitHub Desktop.
Transpose Json data (example)
#!/usr/bin/node
'use strict';
const $Fs = require('fs');
const $Path = require('path');
const $Glob = require('glob');
const { JSDOM } = require('jsdom'); // uninstall when done
const $Beautify = require('js-beautify');
const config = {
srcPath: 'docs-backup/', // use relative path
targetPath: __dirname + '/src/docs/pages/', // absolute path
}
const beautifyOptions = {
indent_size: 2,
end_with_newline: true,
preserve_newlines: false,
max_preserve_newlines: 0,
wrap_line_length: 0,
wrap_attributes_indent_size: 0,
unformatted: ['b', 'em']
};
const categoryDatabase = JSON.parse($Fs.readFileSync('./menudata.json', 'utf8'));
var deleteFolderRecursive = function (path) {
if ($Fs.existsSync(path)) {
$Fs.readdirSync(path).forEach(function (file, index) {
var curPath = path + "/" + file;
if ($Fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
$Fs.unlinkSync(curPath);
}
});
$Fs.rmdirSync(path);
}
};
/*
Scenario
1. scane through the target dir. Get location of file (directory) as the categorization label. file path as id. Aggregate in object as database
- get all the html files under docs-backup( blocks, components, css, layouts, utilities )
2. process each on
3. get content area (div.content.clearfix and under)
4. Fetch title, (title and remove "Timber | " from the string). it will be the label (for now)
5. Add menu hierarchy in the meta info. (captalize initial letter, replace hyphen with space)
6. copy file over to the source (with .yml extension). maintain the folder structure
*/
/**
* YAML front-matter meta info specs
* title, menu_label, layout (documentation), menu_position, markup_language (html)
*/
// 0. Clean the target directory
deleteFolderRecursive(config.targetPath);
$Fs.mkdirSync(config.targetPath);
// 1. Grab html from the source directory
let srcHtmlFilePaths = getDirs(config.srcPath);
// 2 ~ 5
let srcHtmlFileData = {};
for (const _index in srcHtmlFilePaths) {
const _absolutePath = srcHtmlFilePaths[_index];
const _relativePath = $Path.relative(config.srcPath, _absolutePath);
const _id = _relativePath.replace(/\.html/i, '');
srcHtmlFileData[_id] = getFileInText(_id, _absolutePath);
}
// 6
for (const _key in srcHtmlFileData) {
const _yamlData = formatYaml(srcHtmlFileData[_key]);
const _targetFileFullPath = config.targetPath + _key + '.md';
ensureDirectoryExistence(_targetFileFullPath);
$Fs.writeFileSync(_targetFileFullPath, _yamlData);
}
// console.log(srcHtmlFileData[Object.keys(srcHtmlFileData)[0]]);
console.log("Complete. Processed " + Object.keys(srcHtmlFileData).length + " files.");
function getDirs(srcPath) {
return $Glob.sync(`${srcPath}/**/*.html`, { nodir: true });
}
function getFileInText(id, filePath) {
const _rawHTML = $Fs.readFileSync(filePath, 'utf8');
const _htmlTextDom = new JSDOM(_rawHTML);
const _htmlTextDomDocument = _htmlTextDom.window.document;
// data parsing
// remove Timber |
const _title = _htmlTextDomDocument.querySelector('title').innerHTML.replace(/Timber \| /, '').replace(/\:/, ' -');
// get content body
let _contentHtml = _htmlTextDomDocument.querySelector('div.content.clearfix').innerHTML.replace(/<\!\-\- Sidebar \-\->[\s\S]*<\!\-\- Sidebar End \-\->/gm, '');
return {
title: _title,
menu_label: getLabel(id, _title),
layout: 'documentation',
category: getCategory(id),
markup_language: 'html',
relative_path: id,
content: $Beautify.html(_contentHtml, beautifyOptions)
}
}
function getCategory(id) {
// path = path.split('/');
// return (path[0].match(/^index$/i)) ? 'Index' : (path[0].match(/getting-started/i)) ? 'Getting Started' : toTitleCase(path[0].replace(/\-/g, ' '));
if (id in categoryDatabase) {
return categoryDatabase[id].category.split('|');
} else {
return ['No Category'];
}
}
function getLabel(id, title) {
if (id in categoryDatabase) {
return categoryDatabase[id].label;
} else {
return title;
}
}
function toTitleCase(str) {
return str.replace(/\w\S*/g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
}
function formatYaml(pageData) {
let _yamlTextData = '';
_yamlTextData += '---\n';
_yamlTextData += 'title: ' + pageData.title + '\n';
_yamlTextData += 'menu_label: ' + pageData.menu_label + '\n';
_yamlTextData += 'layout: ' + pageData.layout + '\n';
_yamlTextData += 'category: ' + JSON.stringify(pageData.category) + '\n';
_yamlTextData += 'markup_language: ' + pageData.markup_language + '\n';
_yamlTextData += '---\n\n';
_yamlTextData += pageData.content;
return _yamlTextData;
}
function ensureDirectoryExistence(filePath) {
const _dirname = $Path.dirname(filePath);
if ($Fs.existsSync(_dirname)) {
return true;
}
ensureDirectoryExistence(_dirname);
$Fs.mkdirSync(_dirname);
}
#!/usr/bin/node
'use strict';
/**
* Finds links in a given html and aggregates data to an object.
*/
const $Fs = require('fs');
const $Path = require('path');
const { JSDOM } = require('jsdom'); // uninstall when done
const rawHTML = $Fs.readFileSync('./sidemenu', 'utf8');
const navDom = new JSDOM(rawHTML);
const headings = navDom.window.document.querySelectorAll('h4 a');
const subHeadings = navDom.window.document.querySelectorAll('h6');
const links = navDom.window.document.querySelectorAll('li a');
let result = { headings: ['Gettings Started'], subheadings: [], links: [] };
console.log("\nHeadings\n");
for (var value of headings.values()) {
result.headings.push(value.innerHTML.replace(/ <span.*?span>/, ''));
}
console.log("\nSubHeadings\n");
for (var value of subHeadings.values()) {
// console.log(value.innerHTML);
result.subheadings.push('Utilities|' + value.innerHTML);
}
console.log("\nLinks\n");
for (var value of links.values()) {
let _currentItem = {};
_currentItem.item_id = value.getAttribute('href').replace(/\.\.\/(.*?)\.html/, '$1');
_currentItem.label = value.innerHTML;
// console.log(value.parentNode.parentNode.innerHTML);
// console.log(value.innerHTML);
// console.log(_currentItem);
result.links.push(_currentItem);
}
console.log(JSON.stringify(result));
#!/usr/bin/node
// Use https://repl.it/languages/nodejs
/**
* original JSON data format [ { "item_id": "getting-started-timber", "label": "Introduction", "category": "Gettings Started"
},...
* Converts to {"blocks/block-register":{"label":"Register","category":"Blocks"},...
*/
const $Fs = require('fs');
const rawJson = $Fs.readFileSync('./data.json', 'utf8');
const dataObject = JSON.parse(rawJson);
let converteData = {};
for(const _index in dataObject) {
const _currentData = dataObject[_index];
const _id =_currentData.item_id;
converteData[_id] = {
label: _currentData.label,
category: _currentData.category
};
}
console.log(JSON.stringify(converteData));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment