/*
* libreevent - build.js
*
* Created by Janis Hutz 03/09/2023, Licensed under the GPL V3 License
* https://janishutz.com, development@janishutz.com
*
*
*/
const markdownIt = require( 'markdown-it' );
const md2html = new markdownIt();
const fs = require( 'fs' );
const path = require( 'path' );
buildNav( buildDocs() );
function buildNav ( pathObject ) {
console.log( 'building nav ' + pathObject );
let fileStruct = `
Home
Home`;
for ( let item in pathObject ) {
console.log( pathObject[ item ] );
}
}
function buildDocs () {
let allFiles = [];
let directoriesToScan = [];
let files = fs.readdirSync( path.join( __dirname + '/src/' ) );
for ( let file in files ) {
if ( files[ file ].substring( files[ file ].length - 3 ) == '.md' ) {
allFiles.push( handleMD( path.join( __dirname + '/src/' + files[ file ] ) ) );
} else {
directoriesToScan.push( files[ file ] );
}
}
let count = directoriesToScan.length;
while ( count > 0 ) {
for ( let missing in directoriesToScan ) {
let files = fs.readdirSync( path.join( __dirname + '/src/' + directoriesToScan[ missing ] ) );
count -= 1;
for ( let file in files ) {
if ( files[ file ].substring( files[ file ].length - 3 ) == '.md' ) {
allFiles.push( handleMD( path.join( __dirname + '/src/' + directoriesToScan[ missing ] + '/' + files[ file ] ) ) );
} else {
directoriesToScan.push( directoriesToScan[ missing ] + '/' + files[ file ] );
count += 1;
}
}
}
}
return allFiles;
}
function handleMD ( filepath ) {
let fileContent = md2html.render( fs.readFileSync( filepath ).toString() );
for ( let letter in fileContent ) {
if ( fileContent[ letter ] == '<' ) {
if ( fileContent.slice( parseInt( letter ), parseInt( letter ) + 9 ) === '
';
} else if ( link.slice( 0, 2 ) == '&/' ) {
newLink = '';
} else if ( link.slice( 0, 6 ) == '&/' ) {
newLink = '';
} else if ( link.slice( 0, 2 ) == '//' ) {
if ( link.includes( '.' ) ) {
newLink = '';
} else {
newLink = '';
}
} else {
console.error( 'Unsupported link: ' + link );
throw 'INVALID LINK FOUND IN PLUGINS README! Please fix and rerun the script';
}
fileContent = fileContent.slice( 0, parseInt( letter ) ) + newLink + fileContent.slice( parseInt( letter ) + i + 2, parseInt( fileContent.length ) );
}
}
}
return storeHTML( fileContent, filepath );
}
function guessTitle( html ) {
return html.substring( parseInt( html.indexOf( '' ) ) + 4, html.indexOf( '
' ) );
}
function storeHTML( html, filepath ) {
let title = guessTitle( html );
let data = `
${ title } :: docs - libreevent
`;
/*
Transform file path into correct file structure and output file to dist/docs folder
*/
let fileOutputPath = path.join( __dirname + '/dist/docs' );
let pos = filepath.indexOf( 'src' );
let category = filepath.substring( parseInt( pos ) + 4, filepath.length - 3 );
fileOutputPath += '/' + category;
let group = '';
if ( category.lastIndexOf( '/' ) >= 0 ) {
group = category.slice( 0, category.lastIndexOf( '/' ) );
} else {
group = category;
}
try {
fs.mkdirSync( fileOutputPath, { recursive: true } );
} catch ( error ) {
null;
}
fileOutputPath += '/index.html';
fs.writeFileSync( fileOutputPath, data );
return { 'filePath': fileOutputPath, 'title': title, 'group': group };
}