Commit deeab935 authored by Isabella Skořepová's avatar Isabella Skořepová
Browse files

Remove bunch useless of code

parent c593d91f
......@@ -8,8 +8,8 @@ deploy:
- docker
environment: deployment
variables:
HTML_DIR: "/html/www"
SSH_HOST: "uploader@cloud.ok1kvk.cz"
HTML_DIR: '/html/www'
SSH_HOST: 'uploader@cloud.ok1kvk.cz'
only:
- master
script:
......@@ -18,7 +18,7 @@ deploy:
- apt -y install rsync
- rm -rf build
- npm ci
- node ./index.js --contentdir content --staticdir static --themedir theme --builddir build --noserver --nowatch
- node ./index.js --dist
- echo "$DEPLOY_SSH_KEY" > sshkey
- chmod 600 sshkey
- rsync -e 'ssh -i sshkey -o StrictHostKeyChecking=no' -rc --delete build/ $SSH_HOST:$HTML_DIR/
......@@ -28,4 +28,3 @@ cache:
paths:
- generator/
- static/
......@@ -4,7 +4,6 @@ Tento software pro psaní článků NENÍ nutné instalovat.
# Užitečné příkazy
./index.js --help
./index.js --debugarticle articles/2015/10w-mono-zesilovac-tda2003.md
# Jak to zprovoznit na fedoře
......@@ -15,10 +14,10 @@ Tento software pro psaní článků NENÍ nutné instalovat.
# Jak to zprovoznit na windows
* Budeme potřebovat [git](https://git-scm.com/download/win)
* Dále budeme potřebovat [nodejs](https://nodejs.org/en/) - stáhněte si tu verzi,
na které je nápis Stable (pod tím Latest Features).
* U obou nástrojů silně doporučuji neměnit žádná nastavení!
- Budeme potřebovat [git](https://git-scm.com/download/win)
- Dále budeme potřebovat [nodejs](https://nodejs.org/en/) - stáhněte si tu verzi,
na které je nápis Stable (pod tím Latest Features).
- U obou nástrojů silně doporučuji neměnit žádná nastavení!
Po instalaci obou věcí přejděte do nějaké složky pomocí průzkumníka (POZOR! název
této složky ani žádné rodičovské nesmí obsahovat mezery) a klikněte na
......@@ -34,4 +33,4 @@ Nyní by se vám měl otevřít prohlížeč na [localhost:1337](http://localhos
# Jak psát články
Pokud jenom píšete články tak vás zajímá pouze složka content, v níž je soubor
README.md, který obsahuje dodatečné informace
\ No newline at end of file
README.md, který obsahuje dodatečné informace
......@@ -37,8 +37,8 @@ function runNodemonWatch() {
await new Promise(res =>
runBabel({ onExit: res, opts: ['--delete-dir-on-start'] }),
)
const watch = !opts.config.options.nowatch
if (watch) {
const prod = opts.config.prod
if (!prod) {
runBabelWatch()
runNodemonWatch()
} else {
......
var sqliteParser = require('sqlite-parser');
var cli = require('cli');
var options = cli.parse({
contentdir: [null, 'Allows to specify arbitrary content directory.', 'string', 'content'],
sql: ['q', 'SQL querry', 'string']
});
var ast = sqliteParser(options.sql).statement[0];
console.log(JSON.stringify(ast,null,' '));
var getProp = function(prop, article) {
var ret = article;
prop.split('.').forEach(function(p) {
if(ret === undefined) return;
ret = ret[p];
});
return ret;
}
var functions = {
'contains': function(context, args) {
if(args.length !== 2) throw new Error('Wrong number of arguments for function contains');
var prop = evalExpr(context, args[0]);
if(!Array.isArray(prop)) return false;
var val = evalExpr(context, args[1]);
var ret = false;
prop.forEach(function(el) {
if(el == val) ret = true;
})
return ret;
},
'count': function(context, args) {
if(args.length !== 1) throw new Error('Wrong number of arguments for function count');
var prop = evalExpr(context, args[0]);
if(prop === undefined) return 0;
if(!Array.isArray(prop)) return 1;
return prop.length;
}
}
var binaryOperators = {
'and': function(context, left, right) {
return evalExpr(context, left) && evalExpr(context, right);
},
'or': function(context, left, right) {
return evalExpr(context, left) || evalExpr(context, right);
},
'==': function(context, left, right) {
return evalExpr(context, left) == evalExpr(context, right);
},
'>': function(context, left, right) {
return evalExpr(context, left) > evalExpr(context, right);
},
'<': function(context, left, right) {
return evalExpr(context, left) < evalExpr(context, right);
},
'>=': function(context, left, right) {
return evalExpr(context, left) >= evalExpr(context, right);
},
'<=': function(context, left, right) {
return evalExpr(context, left) <= evalExpr(context, right);
},
'!=': function(context, left, right) {
return evalExpr(context, left) != evalExpr(context, right);
},
}
var literals = {
'decimal': function(context, value) {
return Number(value);
},
'string': function(context, value) {
return value;
}
}
var evalExpr = function(context, expr) {
if(expr.type == 'identifier') {
return getProp(expr.name, context);
} else if(expr.type == 'expression' && expr.format == 'binary') {
if(typeof binaryOperators[expr.operation] === 'function') {
return binaryOperators[expr.operation](context, expr.left, expr.right);
} else {
throw new Error('Unsupported binary operator '+expr.operation);
}
} else if(expr.type == 'function') {
if(typeof functions[expr.name] === 'function') {
return functions[expr.name](context, expr.args);
} else {
throw new Error('Unsupported function '+expr.name);
}
} else if(expr.type == 'literal') {
if(typeof literals[expr.variant] === 'function') {
return literals[expr.variant](context, expr.value);
} else {
throw new Error('Unsupported literal variant '+expr.variant);
}
} else {
throw new Error('Unsupported expression '+JSON.stringify(expr));
}
throw new Error('Code should not get here. This is really bad.');
}
var jobs = require('./sitegin/jobs');
jobs.registerMultiple(
{},
['parseHugo', './parseHugo'],
['readFiles', './readFiles']
)
.then(function() {
return jobs.run('readFiles',{config:{
sourceDir: options.contentdir,
articlesLocation: 'articles',
redirectsLocation: 'redirects'
}})
})
.then(function(obj) {
return jobs.run('parseHugo',obj)
})
.then(function(obj){
return obj.pages
})
.then(function(pages) {
return pages.filter(function(page) {
return evalExpr(page, ast.where[0]);
})
})
.then(function(pages) {
var results = ast.result;
if(results[0].variant == 'star') {
pages.forEach(function(page) {
console.log(JSON.stringify(page));
})
return;
}
pages.forEach(function(page) {
var n = {};
results.forEach(function(result) {
if(result.alias) n[result.alias] = getProp(result.name, page);
else n[result.name] = getProp(page, result.name);
})
console.log(n);
})
})
.then(function() {
process.exit(0);
})
.catch(function(e) {
console.log(e.stack);
process.exit(1);
})
/*
* This file specifies whole configuration of sitegin.
* It reads config file from content/config.toml it also parses command-line
* arguments. CLI args take precedence over config options
* It parses command-line arguments. CLI args take precedence over config options
*/
const cli = require('cli')
const fs = require('fs')
const path = require('path')
const toml = require('toml')
const options = cli.parse({
noserver: ['n', 'Dont run server'],
prod: [null, 'Activates production mode'],
port: ['p', 'Port on which server should run', 'number', 1337],
uiport: [null, 'BrowserSync UI port', 'number', 'port+1'],
httpsonly: [null, 'Converts all http://ok1kvk.cz links to https'],
httponly: [null, 'Converts all https://ok1kvk.cz links to http'],
contentdir: [
null,
'Allows to specify arbitrary content directory.',
'string',
'content',
],
themedir: [
null,
'Allows to specify arbitrary content directory.',
'string',
'theme',
],
themesourcedir: [
null,
'Allows to specify arbitrary theme source directory',
'string',
'theme-source',
],
builddir: [
null,
'Allows to specify arbitrary content directory.',
'string',
null,
],
staticdir: [
null,
'Allows to specify arbitrary directory for static files.',
'string',
'static',
],
baseurl: [null, 'BaseURL without trailing /', 'string', ''],
nowatch: [null, 'Disable watching for changes'],
debugarticle: [
null,
'Article for which extra information should be printed out. Full file path ' +
'relative to contentdir (Ex. articles/2010/hello-word.md)',
'string',
null,
],
})
module.exports = () => {
console.log('Build step: Config')
let builddir = 'build'
if (options.builddir) ({ builddir } = options)
else if (!options.noserver) builddir = 'build-debug'
if (options.uiport === 'port+1') {
options.uiport = options.port + 1
}
return new Promise(resolve => {
const config = {
options,
builddir,
themeSourceDir: options.themesourcedir,
sourceDir: options.contentdir,
staticDir: options.staticdir,
themeDir: options.themedir,
articlesLocation: 'articles',
return {
config: {
...options,
uiport: options.port + 1,
builddir: options.prod ? 'build' : 'build-debug',
redirectsLocation: 'redirects',
linksPerPage: 6,
}
const configFile = path.join(config.sourceDir, 'config.toml')
fs.readFile(configFile, 'utf-8', (err, data_) => {
if (err) {
console.log(`Failed to read configuration from ${configFile}`)
console.log('Using defaults')
resolve({ config })
return
}
const data = toml.parse(data_)
for (const attr of Object.keys(config)) {
data[attr] = config[attr]
}
resolve({ config: data })
})
})
},
}
}
module.exports.watch = !options.noserver
module.exports.themeDir = options.themedir
module.exports.options = options
......@@ -94,7 +94,11 @@ module.exports = (href_, title, text, curFile_, cfg) => {
curFile = curFile.replace(/\.md$/, '')
let targetDir = rewriteURL(curFile)
targetDir = path.resolve(cfg.builddir, targetDir)
const absSrc = path.resolve(cfg.staticDir, curFile, href)
const absSrc = path.resolve(
path.join(__dirname, '..', 'static'),
curFile,
href,
)
if (!options.size) options.size = []
let w = options.size[0]
......
......@@ -5,21 +5,16 @@ const config = require('./config')
cli.main(function(args, options) {
console.log(options)
config()
.then(function(obj) {
sitegin(obj.config)
})
.catch(function(e) {
console.log(e.stack)
process.exit()
})
try {
sitegin(config())
} catch (e) {
console.log(e.stack)
process.exit()
}
})
function sitegin(config) {
const options = config.options
const pipeline = require('./sitegin')({
watch: !options.noserver && !options.nowatch,
})
function sitegin({ config }) {
const pipeline = require('./sitegin')
const sass = require('node-sass')
const fs = require('fs')
......@@ -57,7 +52,7 @@ function sitegin(config) {
doSync()
if (!first) {
first = true
copyStaticFiles(config.builddir, config.staticDir)
copyStaticFiles(config.builddir)
}
if (runAgain) run()
})
......@@ -71,16 +66,16 @@ function sitegin(config) {
if (runAgain) run()
if (e.stack) console.log(e.stack)
else console.log(e)
if (config.options.noserver) process.exit(13)
if (config.prod) process.exit(13)
})
}
console.log('Sitegin successfully loaded')
run()
rendersass(config.builddir, config.themeSourceDir)
rendersass(config.builddir)
if (!options.noserver) {
if (!config.prod) {
const sync = require('browser-sync').create()
doSync = function() {
sync.reload('*')
......@@ -91,15 +86,18 @@ function sitegin(config) {
baseDir: config.builddir,
},
ui: {
port: options.port + 1,
port: config.uiport,
},
port: options.port,
port: config.port,
})
const chokidar = require('chokidar')
// article or theme reload
if (!options.nowatch) {
const watches = [path.join(config.sourceDir, 'articles'), config.themeDir]
if (!config.prod) {
const chokidar = require('chokidar')
const watches = [
path.join(__dirname, '..', 'content', 'articles'),
path.join(__dirname, '..', 'theme'),
]
console.log('Watching', watches, 'for changes')
chokidar
.watch(watches, { ignoreInitial: true })
......@@ -112,11 +110,11 @@ function sitegin(config) {
}
}
function copyStaticFiles(builddir, staticdir) {
function copyStaticFiles(builddir) {
// STATIC FILES
const fsextra = require('node-fs-extra')
fsextra.copy(
staticdir,
path.join(__dirname, '..', 'static'),
builddir,
function(file) {
return !(file.match('\\.git') || file.match('static/articles'))
......@@ -125,26 +123,30 @@ function copyStaticFiles(builddir, staticdir) {
console.log('copy static done')
},
)
fsextra.copy(`${staticdir}/articles`, `${builddir}/clanek`, function() {
console.log('copy static/articles done')
})
fsextra.copy(
path.join(__dirname, '..', 'static', 'articles'),
`${builddir}/clanek`,
function() {
console.log('copy static/articles done')
},
)
}
function rendersass(builddir, themesourcedir) {
function rendersass(builddir) {
// SASS
const sass = require('node-sass')
const fs = require('fs')
const mkdirp = require('mkdirp')
sass.render({ file: `${themesourcedir}/sass/style.scss` }, function(
err,
result,
) {
if (err === null) {
console.log('compiled sass')
mkdirp(`${builddir}/theme`, () => {
fs.writeFile(`${builddir}/theme/style.css`, result.css, () => {})
})
} else console.log('error ', err)
})
sass.render(
{ file: path.join(__dirname, '..', 'theme-source', 'sass', 'style.scss') },
function(err, result) {
if (err === null) {
console.log('compiled sass')
mkdirp(`${builddir}/theme`, () => {
fs.writeFile(`${builddir}/theme/style.css`, result.css, () => {})
})
} else console.log('error ', err)
},
)
}
......@@ -51,17 +51,11 @@ renderer._link = renderer.link
module.exports = obj => {
console.log('Build step: Markdown')
const { options } = obj.config
renderer.link = (href_, title, text) => {
let href = href_
if (options.httponly) {
if (href.match(/^https:\/\/ok1kvk.cz/))
href = href.replace('https://', 'http://')
}
if (options.httpsonly) {
if (href.match(/^http:\/\/ok1kvk.cz/))
href = href.replace('http://', 'https://')
}
if (href.match(/^http:\/\/ok1kvk.cz/))
href = href.replace('http://', 'https://')
return renderer._link(href, title, text)
}
......
......@@ -40,24 +40,22 @@ const readPageWorker = (content_, obj, authors) => {
return obj
}
module.exports = obj => {
module.exports = async obj => {
console.log('Build step: ParseHugo')
const authors = new Map()
JSON.parse(
fs.readFileSync(
path.join(obj.config.options.contentdir, 'authors.json'),
path.join(path.join(__dirname, '..', 'content'), 'authors.json'),
'utf8',
),
).forEach(author => {
authors.set(author.name, author)
})
return new Promise(resolve => {
const pages = []
obj.pages.forEach(page => {
pages.push(readPageWorker(page.content, page, authors))
})
// eslint-disable-next-line no-param-reassign
obj.pages = pages
resolve(obj)
const pages = []
obj.pages.forEach(page => {
pages.push(readPageWorker(page.content, page, authors))
})
// eslint-disable-next-line no-param-reassign
obj.pages = pages
return obj
}
......@@ -24,15 +24,13 @@ const readRedirectWorker = (content, obj) => {
return obj
}
module.exports = obj => {
module.exports = async obj => {
console.log('Build step: ParseRedirects')
return new Promise(resolve => {
const redirects = []
obj.redirects.forEach(page => {
redirects.push(readRedirectWorker(page.content, page))
})
// eslint-disable-next-line no-param-reassign
obj.redirects = redirects
resolve(obj)
const redirects = []
obj.redirects.forEach(page => {
redirects.push(readRedirectWorker(page.content, page))
})
// eslint-disable-next-line no-param-reassign
obj.redirects = redirects
return obj
}
/*
* This job reads files from config.articlesLocation and loads this list to `pages`
*/
const walk = require('walk')
const fs = require('fs')
const path = require('path')
......@@ -9,7 +6,7 @@ module.exports = obj => {
console.log('Build step: ReadFiles')
const onFile = (root, fileStats, next, array) => {
const filename = path.join(root, fileStats.name)
const file = path.relative(obj.config.sourceDir, filename)
const file = path.relative(path.join(__dirname, '..', 'content'), filename)
fs.readFile(filename, 'utf8', (err, data) => {
if (err) {
console.log(`Error reading file ${filename}`)
......@@ -28,7 +25,7 @@ module.exports = obj => {
const promise1 = new Promise(resolve => {
const pages = []
walk
.walk(path.join(obj.config.sourceDir, obj.config.articlesLocation))
.walk(path.join(__dirname, '..', 'content', 'articles'))
.on('file', (root, fileStats, next) => {
onFile(root, fileStats, next, pages)
})
......@@ -44,7 +41,7 @@ module.exports = obj => {
const promise2 = new Promise(resolve => {
const redirects = []
walk
.walk(path.join(obj.config.sourceDir, obj.config.redirectsLocation))
.walk(path.join(__dirname, '..', 'content', 'redirects'))
.on('file', (root, fileStats, next) => {
onFile(root, fileStats, next, redirects)
})
......
......@@ -10,18 +10,17 @@ const sitemap = require('./sitemap')
const parseRedirects = require('./parseRedirects')
const configI = require('./config')
module.exports = config => {
return () =>
Promise.resolve(config)
.then(configI)