Skip to content

Commit

Permalink
issue-14 updated
Browse files Browse the repository at this point in the history
  • Loading branch information
juuuuuuun committed Oct 8, 2021
1 parent 72efea5 commit 235ee15
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 117 deletions.
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"
}

0 comments on commit 235ee15

Please sign in to comment.