Skip to content

Commit ce3fdeb

Browse files
committed
Add setup for generating zines.
1 parent 021b4e5 commit ce3fdeb

File tree

6 files changed

+218
-0
lines changed

6 files changed

+218
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

bin/build.js

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const buildHTML = (partialsPath, css) => {
5+
const inputPath = path.join(partialsPath, 'index.html');
6+
let html = fs.readFileSync(inputPath, 'utf-8');
7+
8+
// 1. get all the HTML partial files (if any exist)
9+
// put them in placeholder spots
10+
const partRegExp = new RegExp(/{{ (.*) }}/g);
11+
const partials = html.match(partRegExp);
12+
if (partials) {
13+
partials.forEach((partial) => {
14+
const fileName = partial.replace(/[{|}| ]/g, '');
15+
const filePath = path.join(partialsPath, `${fileName}.html`);
16+
let file = '';
17+
if (!fs.existsSync(filePath)) {
18+
console.error(`Partial ${fileName} missing, skipping`);
19+
} else {
20+
file = fs.readFileSync(filePath, 'utf-8');
21+
}
22+
html = html.replace(partial, file);
23+
});
24+
}
25+
26+
// 2. put css in the <style> tag
27+
// TO-IMPROVE
28+
html = html.replace('<style></style>', `<style>${css}</style>`);
29+
30+
return html;
31+
}
32+
33+
const buildCSS = (cssPath) => {
34+
const inputPath = path.join(cssPath, 'main.css');
35+
let css = fs.readFileSync(inputPath, 'utf-8');
36+
37+
// 0. add variables for colors
38+
// TO-DO
39+
40+
// 1. get all the CSS partials (if any exist)
41+
// put them in placeholder spots
42+
// TO-DO
43+
44+
// 2. remove comments
45+
css = css.replace(/(\/\*.*\*\/)/g, '');
46+
47+
// 3. remove whitespace
48+
// css = css.replace(/\s/g,'');
49+
50+
return css;
51+
}
52+
53+
// build output HTML and overwrite existing output
54+
module.exports = {
55+
build: function(zinePath, partialsPath, cssPath) {
56+
const css = buildCSS(cssPath);
57+
const html = buildHTML(partialsPath, css);
58+
59+
console.log('Building zine files...');
60+
const indexFile = path.join(zinePath, 'index.html');
61+
fs.writeFileSync(indexFile, html);
62+
console.log('Zine built!');
63+
}
64+
}

bin/index.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Zine Title</title>
6+
<style></style>
7+
</head>
8+
<body>
9+
<div class="zine"></div>
10+
</body>
11+
</html>

bin/index.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
const setup = require('./setup.js');
7+
const build = require('./build.js');
8+
9+
// get script arguments
10+
const args = process.argv.slice(2);
11+
12+
// get zine path from arguments
13+
const zinePath = args.shift();
14+
if (!zinePath) {
15+
console.error('Missing zine name!');
16+
}
17+
18+
const cmd = args.shift();
19+
if (!cmd) {
20+
console.error('Missing command for the script!');
21+
}
22+
23+
const cssPath = path.join(zinePath, 'css');
24+
const partialsPath = path.join(zinePath, 'partials');
25+
26+
// watch partials and CSS files for changes, rebuild
27+
const watch = () => {
28+
console.log('\nWatching build files');
29+
fs.watch(partialsPath, (event, file) => {
30+
console.log(`Changes in: ${file}`);
31+
build.build(zinePath, partialsPath, cssPath);
32+
});
33+
fs.watch(cssPath, (event, file) => {
34+
console.log(`Changes in: ${file}`);
35+
build.build(zinePath, partialsPath, cssPath);
36+
})
37+
}
38+
39+
switch(cmd) {
40+
// set up the directory for the zine
41+
case 'setup':
42+
setup.setup(zinePath, partialsPath, cssPath);
43+
break;
44+
// build zine files
45+
case 'build':
46+
build.build(zinePath, partialsPath, cssPath);
47+
break;
48+
// watch and build on changes
49+
case 'dev':
50+
build.build(zinePath, partialsPath, cssPath);
51+
watch();
52+
break;
53+
default:
54+
console.error(`Error: Command ${cmd} not implemented!`)
55+
break;
56+
}
57+
58+
59+
60+
// console.log(process.argv.slice(2));
61+
// text.split(/\r?\n/).forEach(function (line) {
62+
// console.log(line);
63+
// });
64+
// const text = fs.readFileSync('kitten', "utf8");
65+
// fs.appendFileSync('kitten', '456', 'utf8');
66+
// remove previous output file before generating (not necessary, overwritten)
67+
// fs.unlinkSync(indexFile);
68+
69+
// const zineDir = path.parse(zinePath);
70+
// const outputFile = zinePath.name();

bin/main.css

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
@font-face {
2+
font-family: 'threemedium';
3+
src: url('https://alicja.dev/assets/fonts/three.eot');
4+
src: url('https://alicja.dev/assets/fonts/three.eot?#iefix') format('embedded-opentype'),
5+
url('https://alicja.dev/assets/fonts/three.woff2') format('woff2'),
6+
url('https://alicja.dev/assets/fonts/three.woff') format('woff'),
7+
url('https://alicja.dev/assets/fonts/three.ttf') format('truetype'),
8+
url('https://alicja.dev/assets/fonts/three.svg#threemedium') format('svg');
9+
font-weight: normal;
10+
font-style: normal;
11+
}
12+
13+
body {
14+
font: 18px Verdana, sans-serif;
15+
font: 36px "threemedium";
16+
}
17+
18+
.zine {
19+
width: 840px;
20+
margin: 0 auto;
21+
}
22+
23+
/* COLOURS
24+
purple-0: #8f4898
25+
purple-1: #a846a0
26+
purple-2: #c45ab3
27+
pink-0: #ee4266
28+
orange: #ff6700
29+
pink-1: #ffa69e
30+
yellow: #fed766
31+
green: #23ce6b
32+
teal: #3bceac
33+
blue: #009fb7
34+
grey-0: #45454e
35+
grey-1: #b2b3b8
36+
grey-2: #c7c5c6
37+
white: #e8e6e7
38+
h-purple: #ce97fb
39+
h-pink: #f6a5eb
40+
h-orange: #faa99d
41+
h-yellow: #fddf7e
42+
h-teal: #9bfbe1
43+
h-blue: #67ebfa
44+
*/
45+
46+
@media print {
47+
@page { size: auto portrait; margin: 0cm; }
48+
.zine { margin: 1cm }
49+
}

bin/setup.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const initialHTML = fs.readFileSync(path.join(__dirname, '/index.html'));
5+
const initialCSS = fs.readFileSync(path.join(__dirname, './main.css'));
6+
7+
module.exports = {
8+
setup: function(zinePath, partialsPath, cssPath) {
9+
if (!fs.existsSync(zinePath)) {
10+
fs.mkdirSync(zinePath);
11+
}
12+
13+
if (!fs.existsSync(partialsPath)) {
14+
fs.mkdirSync(partialsPath);
15+
}
16+
fs.writeFileSync(path.join(partialsPath, 'index.html'), initialHTML);
17+
18+
if (!fs.existsSync(cssPath)) {
19+
fs.mkdirSync(cssPath);
20+
}
21+
fs.writeFileSync(path.join(cssPath, 'main.css'), initialCSS);
22+
}
23+
}

0 commit comments

Comments
 (0)