Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-14 updated #18

Merged
merged 1 commit into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ node_modules/
dir1/
dist/
*.txt
*.md
*.md
sample
build/
222 changes: 126 additions & 96 deletions bin/osdssg.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,22 @@ const takeFile = () => {
return yargs.argv._[0];
};

const addingStyleSheet = (parsedHtml) => {
if (command.s) {
const addingStyleSheet = (parsedHtml, css) => {
if (command.s || css) {
const styleSheet = css ?? command.s;
let head = parsedHtml.querySelector("head");
head.appendChild(parse(`<link href="${command.s}" rel="stylesheet" />`));
if(styleSheet){
head.appendChild(parse(`<link href="${styleSheet}" rel="stylesheet" />`));
}

}
};

const updatingLang = (parsedHtml) => {
if (command.l) {
if (command.l.length > 0 && command.l.length !== undefined) {
parsedHtml.childNodes[1].rawAttrs = `lang="${command.l}"`;
const updatingLang = (parsedHtml, lang) => {
if (command.l || lang) {
const language = lang ?? command.l;
if (language && language.length > 0) {
parsedHtml.childNodes[1].rawAttrs = `lang="${language}"`;
return console.log(`Language has been set`);
}
return console.log("Please add chosen language after flag. e.g: -f fr");
Expand Down Expand Up @@ -84,60 +89,130 @@ cli = cli
.option("lang", {
alias: "l",
desc: "Add an option flag to indicates the language of the html element. e.g: --lang fr is lang='fr' means using French"
})
.option("config", {
alias: "c",
desc: "Add an option flag to indicates the config for replacing using command line argument. e.g: --config ./ssg-config.json means using ./ssg-config.json's values for command line argument."
}).argv;

const command = yargs.argv;
console.log(command)
//moved if condition to here and added config identify opt
if(!command.c && command.config && !command.i && !command.input){
return console.log(
`Please enter file name or folder e.g: --input text.txt or --config ssg-config.json`
)
}

if (command.i || command.input) {
if (command._.length <= 0) {
return console.log(
"Please enter file name or folder e.g: --input text.txt"
);
let fileOrDir;
let outputDir;
let lang;
let css;
if(command.c || command.config){
//check if the file is json
if(!command.c.endsWith("json") || !command.config.endsWith("json")){
return console.log("Sorry your input file is not json type.")
}
const jsonData = require(`../${command.c}`);
console.log(jsonData);
fileOrDir = jsonData.input;
outputDir = (jsonData.output && jsonData.output.replace('./', '')) ?? 'dist';
lang = jsonData.lang;
css = jsonData.styleSheet;
//ignore all options
if(!fileOrDir){
console.log('Please provide -c or -i options');
}
}else {
fileOrDir = takeFile();
}

fs.readFile(
path.join(__dirname, "htmlTemplate.html"),
"utf-8",
(err, html) => {
if (err) return console.log(err);
fs.readFile(
path.join(__dirname, "htmlTemplate.html"),
"utf-8",
(err, html) => {
if (err) return console.log(err);

const fileOrDir = takeFile();
if (!fs.existsSync(fileOrDir)) {
return console.log("No File or Directory Found");
}

if (!fs.existsSync(fileOrDir)) {
return console.log("No File or Directory Found");
}
const stats = fs.statSync(fileOrDir);

const stats = fs.statSync(fileOrDir);
const dir = path.join(process.cwd(), outputDir);

const dir = path.join(process.cwd(), "dist");
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
} else {
fs.rmdirSync(dir, { recursive: true });
fs.mkdirSync(dir);
}

if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
} else {
fs.rmdirSync(dir, { recursive: true });
fs.mkdirSync(dir);
if (stats.isFile()) {
if (!fileOrDir.includes(".txt") && !fileOrDir.includes(".md")) {
return console.log(
"Only .txt files and .md files can be supported in this tool!"
);
}

if (stats.isFile()) {
if (!fileOrDir.includes(".txt") && !fileOrDir.includes(".md")) {
return console.log(
"Only .txt files and .md files can be supported in this tool!"
const isMDfile = fileOrDir.endsWith(".md");
fs.readFile(`${fileOrDir}`, "utf-8", (error, data) => {
if (error) return console.log(error);

let parsedHtml = parse(html);

addingStyleSheet(parsedHtml, css);
updatingLang(parsedHtml, lang);

let title;
let bodyPart;
let body = parsedHtml.querySelector("body");

if (isMDfile) {
// if it is md file, the title will be the first h1
bodyPart = textToPMd(data);
body.appendChild(parse(bodyPart));
title = body.querySelector("h1")?.text || "Document";
} else {
title = getTitle(data);
bodyPart = textToP(data);
body.appendChild(parse(bodyPart));
body.querySelector("p").replaceWith(parse(`<h1>${title}</h1>`));
}
parsedHtml.querySelector("title").set_content(title);
if (
!fs.existsSync(
path.join(process.cwd(), outputDir, `${fileOrDir}.html`)
)
) {
fs.writeFile(
`${process.cwd()}/${outputDir}/index.html`,
parsedHtml.toString(),
(e) => {
if (e) return console.log(e);
console.log("New file has been created!!");
}
);
}

const isMDfile = fileOrDir.endsWith(".md");
fs.readFile(`${fileOrDir}`, "utf-8", (error, data) => {
});
} else {
const files = fs.readdirSync(fileOrDir);
if (files.length <= 0) {
return console.log("There is no file in the directory");
}
let count = 0;
files.forEach((file) => {
const isMDfile = file.endsWith(".md");
fs.readFile(path.join(fileOrDir, file), "utf-8", (error, data) => {
if (error) return console.log(error);

let parsedHtml = parse(html);

addingStyleSheet(parsedHtml);
updatingLang(parsedHtml);
addingStyleSheet(parsedHtml, css);
updatingLang(parsedHtml, lang);

let title;
let bodyPart;
let body = parsedHtml.querySelector("body");

if (isMDfile) {
// if it is md file, the title will be the first h1
bodyPart = textToPMd(data);
Expand All @@ -149,70 +224,25 @@ if (command.i || command.input) {
body.appendChild(parse(bodyPart));
body.querySelector("p").replaceWith(parse(`<h1>${title}</h1>`));
}

parsedHtml.querySelector("title").set_content(title);
if (
!fs.existsSync(
path.join(process.cwd(), "dist", `${fileOrDir}.html`)
)
) {
const pathName = `${process.cwd()}/${outputDir}/${file}.html`;

if (!fs.existsSync(pathName)) {
count++;

fs.writeFile(
`${process.cwd()}/dist/index.html`,
path.join(process.cwd(), outputDir, `index${count}.html`),
parsedHtml.toString(),
(e) => {
if (e) return console.log(e);
console.log("New file has been created!!");
console.log(`New file has been created in ${outputDir} directory!!`);
}
);
}
});
} else {
const files = fs.readdirSync(fileOrDir);
if (files.length <= 0) {
return console.log("There is no file in the directory");
}
let count = 0;
files.forEach((file) => {
const isMDfile = file.endsWith(".md");
fs.readFile(path.join(fileOrDir, file), "utf-8", (error, data) => {
if (error) return console.log(error);
let parsedHtml = parse(html);

addingStyleSheet(parsedHtml);
updatingLang(parsedHtml);

let title;
let bodyPart;
let body = parsedHtml.querySelector("body");
if (isMDfile) {
// if it is md file, the title will be the first h1
bodyPart = textToPMd(data);
body.appendChild(parse(bodyPart));
title = body.querySelector("h1")?.text || "Document";
} else {
title = getTitle(data);
bodyPart = textToP(data);
body.appendChild(parse(bodyPart));
body.querySelector("p").replaceWith(parse(`<h1>${title}</h1>`));
}

parsedHtml.querySelector("title").set_content(title);
const pathName = `${process.cwd()}/dist/${file}.html`;

if (!fs.existsSync(pathName)) {
count++;

fs.writeFile(
path.join(process.cwd(), "dist", `index${count}.html`),
parsedHtml.toString(),
(e) => {
if (e) return console.log(e);
console.log("New file has been created in dist directory!!");
}
);
}
});
});
}
});
}
);
}
}
);

40 changes: 20 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions ssg-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"input":"sample",
"output":"build",
"style":"https://cdn.jsdelivr.net/npm/water.css@2/out/water.css",
"lang":"en",
"future-function":"for future"
}