Skip to content

Commit

Permalink
adds support for lang, chngd the fxn args updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ritikbheda committed Oct 2, 2021
1 parent ba6af98 commit d180222
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@ Commandline Static Site Generating tool that helps you produce `.html` from `.tx

## Features
The tool is before its first release but comes up with these features already:
* accept `.txt` file to generate a `.html` file
* accept folders to generate `.html` files from `.txt` files
* accept `.txt` or `.md` file to generate a `.html` file
* accept folders to generate `.html` files from `.txt` and `.md` files
* goes through all the child directories of the input folder to generate the desired files
* output folder can be externally provided to get the generated files at desired location. if not provided, the output is generated in a folder set as default output folder
* stylesheet can be externally provided in either link form or file form to add style to the webpages. If not provided, the no stylesheet is added to the webpages
* detects title and paragraph of the text file to give accurate tags in the webpage and sets title to the webpage
* if a folder is provided, a new file called `index.html` is generated which contains link to all the files just generated
* can generate document for any language under [MDN WebDocs](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/lang)

The tool can now also process `.md` files:
* accept `.md` file to generate a `.html` file
* accept folders to generate `.html` files from `.md` files as well (processes both `.txt` and `.md` in a folder)
* can detect headings h1-h6 and converts them to the respective heading tag. For example:
`## Heading 2` gets converted to `<h2>Heading 2<h2>`
* generates paragraphs for text which is not a heading
* detects and processes **bold** text using the double `**` or `__` syntax
## Usage
### Options
Option | Function
Expand Down
27 changes: 14 additions & 13 deletions src/fileFolder/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ function isTxtFile(file){
return false;
}

function parseFile(filename, destination, stylesheet){
function parseFile(options){
/**
* Checks if the file is txt or md
* Parses the file and generates html if the extension is supported
* Logs an error message if file format is invalid
*/
if(path.extname(filename) == '.txt'){
parseTxttoHTML(filename, destination, stylesheet);
if(path.extname(options.input) == '.txt'){
parseTxttoHTML(options);
}
else if(path.extname(filename) == '.md'){
parseMdToHTML(filename, destination, stylesheet);
else if(path.extname(options.input) == '.md'){
parseMdToHTML(options);
}
else{
console.log(`"${path.basename(filename)}" is not a supported format. Please use a .txt or .md file!`)
console.log(`"${path.basename(options.input)}" is not a supported format. Please use a .txt or .md file!`)
}
}

function parseMdToHTML(filename, destination, stylesheet){
function parseMdToHTML(options){
/**
* create new html file at the destination
* Parse the data from md file and add it to the html file
*/

fs.readFile(filename, 'utf-8', function(err, data){
const {filename, destination, stylesheet, lang} = options;
fs.readFile(filename, 'utf-8', function(err, data){

if(err){
console.error(err);
Expand Down Expand Up @@ -64,7 +64,7 @@ function parseMdToHTML(filename, destination, stylesheet){
.replace(/__(\S[\s\S]*?)__/gim, '<strong>$1</strong>')
.replace(/\*\*(\S[\s\S]*?)\*\*/gim, '<strong>$1</strong>');

const toReturn = `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>${title}</title>${stylesheet ? `<link rel="stylesheet" href="${stylesheet}">` : ''}<meta name="viewport" content="width=device-width, initial-scale=1"></head><body>${htmlBody}</body></html>`;
const toReturn = `<!doctype html><html lang="${lang}"><head><meta charset="utf-8"><title>${title}</title>${stylesheet ? `<link rel="stylesheet" href="${stylesheet}">` : ''}<meta name="viewport" content="width=device-width, initial-scale=1"></head><body>${htmlBody}</body></html>`;
const newFilePath = path.join(process.cwd(), destination, path.basename(filename, '.md')) + ".html";

fs.writeFile(newFilePath, toReturn, function (err){
Expand All @@ -74,13 +74,14 @@ function parseMdToHTML(filename, destination, stylesheet){
})
}

function parseTxttoHTML(filename, destination, stylesheet){
function parseTxttoHTML(options){

/**
* Create new html file at the destination
* Parse the data from txt file and add it to the html file
*/
fs.readFile(filename, 'utf-8', function(err, data){
const {input: filename, output: destination, stylesheet, lang} = options;
fs.readFile(filename, 'utf-8', function(err, data){

if(err){
console.error(err);
Expand All @@ -97,7 +98,7 @@ function parseTxttoHTML(filename, destination, stylesheet){
`<p>${para.replace(/\r?\n/, ' ')}</p>`
).join(' ');

const toReturn = `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>${title}</title><link rel="stylesheet" href="${stylesheet}"><meta name="viewport" content="width=device-width, initial-scale=1"></head><body><h1>${title}</h1> ${html} </body></html>`;
const toReturn = `<!doctype html><html lang="${lang}"><head><meta charset="utf-8"><title>${title}</title><link rel="stylesheet" href="${stylesheet}"><meta name="viewport" content="width=device-width, initial-scale=1"></head><body><h1>${title}</h1> ${html} </body></html>`;
const newFilePath = path.join(process.cwd(), destination, path.basename(filename, '.txt')) + ".html";

fs.writeFile(newFilePath, toReturn, function (err){
Expand Down
5 changes: 3 additions & 2 deletions src/fileFolder/folder.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ function loopThroughAllFiles(options, otherFolderPath= ""){ // this is a recursi
fs.readdirSync(folderPath).forEach(filename => {
const stats = fs.statSync(path.join(folderPath, filename));
if(stats.isFile()){
file.parseFile(path.join(folderPath, filename), options.output, options.stylesheet);
options.input = path.join(folderPath, filename);
file.parseFile(options);

let fileBaseName = '';
if(path.extname(filename) == '.txt') fileBaseName = path.basename(filename, '.txt');
Expand All @@ -40,7 +41,7 @@ function createDestination(destination){
*/

try{
fs.rmSync(path.join(process.cwd(), destination), {recursive: true}, (err) =>{
fs.rmdirSync(path.join(process.cwd(), destination), {recursive: true}, (err) =>{
if(err){
throw err;
}
Expand Down
6 changes: 3 additions & 3 deletions src/fileFolder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ function fileFolder(options){

const stats = fs.statSync(options.input);
if(stats.isFile()){
const filename = process.cwd() +'\\'+ options.input;
file.parseFile(filename, options.output, options.stylesheet);
options.input = process.cwd() +'\\'+ options.input;
file.parseFile(options);
}
else{

const indexFilePath = path.join(process.cwd(), options.output, 'index.html');
const toReturn = `<!doctype html><html lang="en"><head><meta charset="utf-8"><title>Index file</title><link rel="stylesheet" href="${options.stylesheet}"><meta name="viewport" content="width=device-width, initial-scale=1"></head><body>`;
const toReturn = `<!doctype html><html lang="${options.lang}"><head><meta charset="utf-8"><title>Index file</title><link rel="stylesheet" href="${options.stylesheet}"><meta name="viewport" content="width=device-width, initial-scale=1"></head><body>`;

fs.writeFileSync(indexFilePath, toReturn, function (err){
if(err) throw err;
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ program.version(pjson.version, '-v, --version','shows the current version of the

program
.option("-i, --input <type>", "enter the file/folder name to generate its HTML files")
.option("-o, --output <type>", "to enter the output folder", "dist")
.option("-s, --stylesheet <type>", "to enter the stylesheet URL/file", "")
.option("-o, --output <type>", "enter the output folder", "dist")
.option("-s, --stylesheet <type>", "enter the stylesheet URL/file", "")
.option("-l, --lang <type>", "enter the language of the document", "en-CA")
.action((options) =>{
fileFolder(options)
});
Expand Down

0 comments on commit d180222

Please sign in to comment.