Commit e1a79c8c authored by Isabella Skořepová's avatar Isabella Skořepová

Initial commit

parents
node_modules
coverage
build
build-debug
content
theme
static
#!/bin/bash
if [ ! -f content ]
then
echo "Downloading content"
git clone https://git.ok1kvk.cz/ok1kvk.cz/content
else
cd content
git pull origin master
cd ..
fi
if [ ! -f theme ]
then
echo "Downloading theme"
git clone https://git.ok1kvk.cz/ok1kvk.cz/theme
fi
if [ ! -f theme ]
then
echo "Downloading static files"
git clone https://git.ok1kvk.cz/ok1kvk.cz/static
fi
npm install
var input = new (require("./sitegin/input-hugo"))(process.cwd()+'/content/');
var transformerMD = new (require('./sitegin/transformer-markdown.js'));
var transformerTags = new (require('./sitegin/transformer-tags.js'));
var transformerNunjucks = new (require('./sitegin/transformer-nunjucks.js'));
var transformerUrlizeTags = new (require('./sitegin/transformer-urlizetags.js'));
var output = new (require('./sitegin/output-filesystem.js'));
var sequencer = new (require('./sitegin/sequencer'));
var transformerGit = new (require('./sitegin/transformer-git'));
var sass = require('node-sass');
var fs = require('fs');
var builddir = "build";
if(process.argv[2] !== "compileonly") builddir = "build-debug";
var oldEmit = sequencer.emit;
sequencer.emit = function() {
console.log("Event:",arguments[0]);
oldEmit.apply(sequencer, arguments);
}
sequencer.on("", function(ev) {
console.log("Event:",ev);
})
var tasks = 0;
var onDone = function() {
tasks--;
if(tasks == 0 && process.argv[2] == "compileonly") {
console.log("exitting");
process.exit();
}
}
// PIPELINE
sequencer
.load(input)
.registerNext(transformerGit)
.registerNext(transformerMD)
.registerNext(transformerUrlizeTags)
.registerNext(transformerTags)
.registerNext(transformerNunjucks)
.registerNext(output)
.finish(function(list) {
console.log('sequencer done');
onDone();
})
tasks++;
try {fs.mkdirSync(builddir);}catch(e){}
try {fs.mkdirSync(builddir+"/theme");}catch(e){}
// SASS
var rendersass = function() {
sass.render ({file: "theme/sass/style.scss"},function(err, result) {
if(err == null) {
console.log("compiled sass");
fs.writeFile(builddir+"/theme/style.css",result.css);
} else console.log("error ", err);
console.log("sass done");onDone();
});
};
tasks++;
rendersass();
// STATIC FILES
var fsextra = require('node-fs-extra');
tasks+=2;
fsextra.copy('static',builddir,function(){console.log("copy static done");onDone();});
fsextra.copy('theme/static',builddir,function(){console.log("copy theme static done");onDone();});
// DEVEL INCOMING
if(process.argv[2] !== "compileonly") {
var express = require("express");
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
// WEBSERVER
var port = 1337;
app.use(express.static(builddir)); //use static files in ROOT/builddir folder
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(port, function() {
console.log('listening on *:'+port);
});
// CHANGE WATCHER
var chokidar = require('chokidar');
chokidar.watch('theme/static', {ignoreInitial: true, usePolling: true})
.on('all', (event, path) => {
console.log("theme static copy watcher");
fsextra.copy('theme/static',builddir,function(){});
io.emit('reload');
});
chokidar.watch('static/', {ignoreInitial: true, usePolling: true})
.on('all', (event, path) => {
console.log("static copy watcher");
fsextra.copy('static',builddir,function(){});
io.emit('reload');
});
chokidar.watch(['theme/','content/'], {ignoreInitial: true, usePolling: true})
.on('all', (event, path) => {
if(path.match(/^theme\/static\/.*$/)) return;
if(path.match(/^theme\/sass\/.*$/)) return;
console.log("rebuild watcher",path);
sequencer.load(input);
});
sequencer.finish(function() {
io.emit('reload');
});
chokidar.watch('theme/sass', {ignoreInitial: true, usePolling: true})
.on('all', function(event, path) {
console.log("sass: file change detected - rerendering "+event+" "+path);
rendersass();
io.emit('reload');
});
}
{
"name": "new.ok1kvk.cz",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- sitegin/test/ --recursive -R spec -u exports -t 60000"
},
"author": "",
"license": "BSD",
"dependencies": {
"chokidar": "^1.4.2",
"express": "^4.13.4",
"marked": "^0.3.5",
"mkdirp": "^0.5.1",
"moment": "^2.11.2",
"node-fs-extra": "^0.8.1",
"node-sass": "^3.4.2",
"nodegit": "^0.11.1",
"nunjucks": "^2.3.0",
"nunjucks-date-filter": "^0.1.1",
"socket.io": "^1.4.5",
"toml": "~2.3.0",
"toml-js": "0.0.8"
},
"devDependencies": {
"istanbul": "^0.4.2",
"mocha": "^2.3.4",
"unit.js": "^2.0.0"
}
}
exports.Page = function() {
this.file = "";
this.metadata = {};
this.content = "";
this.type = "article";
}
exports.PageList = function() {
var list = this;
list.list = [];
list.length = 0;
list.addPage = function(article) {
list.length++;
if(! (article instanceof exports.Page))
throw "article has to be of type Page";
else if(list.containsFile(article.file)) {
console.log(list.getPageByFile(article.file));
throw "article is already in this list"
} else
list.list.push(article);
}
list.forEach = function(f) {
list.list.forEach(f);
}
list.getPageByFile = function(file) {
var rarticle = undefined;
list.forEach(function(article) {
if(article.file == file) {
rarticle = article;
}
});
return rarticle;
}
list.containsFile = function(file) {
var contains = false;
list.forEach(function(article) {
if(article.file == file) {
contains = true;
}
});
return contains;
}
return list;
}
var fs = require('fs');
var toml = require('toml');
var PageList = require('./Page').PageList;
var Page = require('./Page').Page;
var moment = require('moment');
var path = require('path');
var subcalls = 0;
function walk(currentDirPath, callback, end_cb) {
subcalls++;
var fs = require('fs'), path = require('path');
fs.readdir(currentDirPath, function(err, files) {
if(err!=null) console.log(err);
files.forEach(function(name) {
var filePath = path.join(currentDirPath, name);
var stat = fs.statSync(filePath);
if (stat.isFile()) {
callback(filePath, stat);
} else if (stat.isDirectory()) {
if(filePath.indexOf(".git") < 0)
walk(filePath, callback, end_cb);
}
});
subcalls--;
if(subcalls <= 0) {
end_cb();
}
});
};
var readPageWorker = function(file, articleName, contentDir) {
if(file === undefined) throw new Error("file is undefined");
file = file.substring(4);
var fm_end = file.indexOf("+++");
var obj = new Page;
obj.origFile = contentDir+articleName+'.md';
obj.file = articleName.replace("articles","clanek");
obj.content = file.substring(fm_end+4);
try {
obj.metadata = toml.parse(file.substring(0,fm_end-1));
} catch(e) {
console.log(articleName+": Failed to parse metadata");
throw e;
}
if(obj.metadata.date) obj.metadata.date = moment(obj.metadata.date);
return obj;
}
module.exports = function(_contentDir) {
var exports = this;
exports.fancyname = "loader hugo";
var contentDir = _contentDir;
if(contentDir == undefined) contentDir = './content/';
if(!contentDir.endsWith('/')) contentDir+='/';
/*
* articleName:
* string
* callback:
* called when article is read
* returns:
* {
* string articleName,content
* object frontmatter
* function save
* }
*/
exports.readPage = function(articleName, cb) {
fs.readFile(contentDir+articleName+".md", 'utf8', function(err, data) {
if(err != null) throw err;
cb(readPageWorker(data, articleName, contentDir));
});
}
/*
* callback:
* function(articleName)
* called for each article on website
* cb_end:
* function()
* called when all articles are listed
*/
exports.forEachPage = function(callback, cb_end) {
walk(contentDir+"articles",function(filePath, stat) {
if(callback !== undefined) callback(filePath.substring(contentDir.length,filePath.length-3));
}, function() {
if(cb_end !== undefined) cb_end();
});
};
/*
* cb:
* function(articleList)
* called after all cb_on_each
*/
exports.getPageList = function(cb) {
var currentlyReadingCount = 0;
var allPagesListed = false;
var cb_called = false;
var list = new PageList;
exports.forEachPage(function(articleName) {
currentlyReadingCount++;
exports.readPage(articleName, function(article){
list.addPage(article);
currentlyReadingCount--;
if(allPagesListed && currentlyReadingCount == 0 && !cb_called) {
if(cb !== undefined) cb(list);
cb_called = true;
}
});
},function(){
if(currentlyReadingCount == 0 && !cb_called) {
if(cb !== undefined) cb(list);
cb_called = true;
}
allPagesListed = true;
});
};
}
module.exports = function() {
var out = this;
out.fancyname = "output console";
out.forEachPage = function(article, cb) {
cb();
}
}
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var builddir = "build";
if(process.argv[2] !== "compileonly") builddir = "build-debug";
module.exports = function() {
var out = this;
out.fancyname = "output filesystem";
out.forEachPage = function(article, cb) {
mkdirp(path.dirname(builddir+'/'+article.file+"/index.html"), () => {
var file = builddir+'/'+article.file+"/index.html";
fs.writeFile(file, article.content, function(err){
if(err) throw err;
cb();
});
});
}
}
module.exports = function() {
var t = this;
var lastEvent;
this.load = function(input) {
lastEvent = input.fancyname+" done";
input.getPageList(function(list) {
t.emit(input.fancyname+" done", list);
});
return this;
}
this.registerNext = function(obj) {
// pre
t.on(lastEvent, function(list) {
if(obj.pre !== undefined) {
obj.pre(function() {
t.emit(obj.fancyname+' pre done', list);
}, list);
} else {
t.emit(obj.fancyname+' pre done', list);
}
});
// forEachPage
t.on(obj.fancyname+' pre done', function(list) {
var transforming = list.length;
if(obj.forEachPage !== undefined) {
list.forEach(function(article) {
obj.forEachPage(article, function() {
transforming--;
if(transforming == 0) {
t.emit(obj.fancyname+' forEachPage done', list);
}
});
});
} else {
t.emit(obj.fancyname+' forEachPage done', list);
}
});
// post
t.on(obj.fancyname+' forEachPage done', function(list) {
if(obj.post !== undefined) {
obj.post(function() {
t.emit(obj.fancyname+' post done', list);
}, list)
} else {
t.emit(obj.fancyname+' post done', list);
}
});
lastEvent = obj.fancyname+' post done';
return this;
}
this.finish = function(f) {
t.on(lastEvent, f);
}
}
module.exports.prototype.__proto__ = require('events').EventEmitter.prototype;
module.exports = function() {
var tr = this;
tr.fancyname = "transformer basichtml";
tr.forEachPage = function(article, cb) {
article.content = "<!DOCTYPE html><html><meta charset=\"UTF-8\" /><head><title>"
+article.metadata.title+
"</title></head><body>" + article.content + "</body></html>";
cb();
}
}
// Add git commit author and date to metadata
var git = require('nodegit');
var path = require('path');
var pathToRepo = path.resolve("content");
var getCommits = function(file, cb) {
git.Repository.open(pathToRepo)
.then(function(r) {
repo = r;
return repo.getMasterCommit();
})
.then(function(firstCommitOnMaster){
walker = repo.createRevWalk();
walker.push(firstCommitOnMaster.sha());
walker.sorting(git.Revwalk.SORT.Time);
return walker.fileHistoryWalk(file, 500);
})
.then(function(resultingArrayOfCommits) {
if(resultingArrayOfCommits.length == 0) {
cb(undefined);
return;
}
var commits = [];
resultingArrayOfCommits.forEach(function(e) {
commits.push(e.commit);
});
cb(commits);
})
.done();
}
module.exports = function() {
var tr = this;
tr.fancyname = "transformer git";
tr.pre = function(cb, articleList) {
var todo = articleList.length;
articleList.forEach(function(article) {
var file = path.relative(pathToRepo, article.origFile);
getCommits(file, function(commits) {
if(commits !== undefined) {
var newestCommit = commits[0];
var oldestCommit = commits[commits.length-1];
if(newestCommit != newestCommit) {
console.log("Newest commit:");
printCommit(newestCommit);
}
article.metadata.date = {
creation: oldestCommit.date(),
modification: newestCommit.date(),
modified: newestCommit !== oldestCommit
}
article.metadata.author = {
name: oldestCommit.author().name(),
email: oldestCommit.author().email()
}
article.commits = commits;
}
todo--;
if(todo == 0) cb();
});
});
}
tr.forEachPage = function(article, cb) {
cb();
}
}
var marked = require('marked');
var renderer = new marked.Renderer();
renderer.image = function(href, title, text) {
if(process.argv[3] !== "final") {
if(!href.match("://")) {
href = "https://ok1kvk.cz"+href;
}
}
var out = '<img src="' + href + '" alt="' + text + '"';
if (title) {
out += ' title="' + title + '"';
}
out += this.options.xhtml ? '/>' : '>';
return out;
};
marked.setOptions({
renderer: renderer,
gfm: true,
tables: true,
breaks: false,
pedantic: false,
sanitize: false,
smartLists: true,
smartypants: false,
});
module.exports = function() {
var tr = this;
tr.fancyname = "transformer markdown";
tr.forEachPage = function(article, cb) {
article.origContent = article.content;
article.content = marked(article.content);
cb();
}
}
var nunjucks = require('nunjucks');
var fs = require('fs');
var dateFilter = require('nunjucks-date-filter');
var MyLoader = nunjucks.Loader.extend({
fileSystemLoader: {},
init: function(searchPaths, opts) {
fileSystemLoader = new nunjucks.FileSystemLoader(searchPaths, opts);
},
getSource: function(name) {
return fileSystemLoader.getSource(name+".html.nunjucks");
}
});
module.exports = function() {
var tr = this;
var templates = {};
tr.pre = function(cb) {
var env = new nunjucks.Environment(new MyLoader('theme/templates',{watch:true}));
env.addFilter('date', dateFilter);
templates["article"] = env.getTemplate('article');
templates["tag"] = env.getTemplate('tag');
templates["redirect"] = env.getTemplate('redirect');
cb();
};
tr.fancyname = "transformer nunjucks";
tr.forEachPage = function(page, cb) {
page.config = {
baseurl: "",
devel: process.argv[2] !== "compileonly"
}
var html = templates[page.type].render(page);
//if(page.type == "tag") console.log(page.content[0].file);
page.content = html;
cb();
}
}
var Page = require("./Page.js").Page;
var moment = require('moment');
module.exports = function() {
var tr = this;
tr.fancyname = "transformer tags";
tr.linksPerPage = 6;
tr.pre = function(cb, list) {
var tags = new Array();
var tagPages = {};
list.forEach(function(article) {
if(article.metadata.tags)
article.metadata.tags.forEach(function(tag) {
if(tags.map(function(e) { return e.url; }).indexOf(tag.url) < 0)
tags.push(tag)
if(tagPages[tag.url] == undefined) tagPages[tag.url] = new Array();
tagPages[tag.url].push(article)
})
});
var generateTagPages = function(file, pageList) {
var p = new Page();
p.type = "redirect";
p.file = file;
p.content = file+"/1";
list.addPage(p);
pageList.sort(function (a, b) {
var rhs = a.metadata.date.creation;
var lhs = b.metadata.date.creation;
return lhs > rhs ? 1 : lhs < rhs ? -1 : 0;
})
var c = 0;
var p = 0;
var tagPage;
var prevurl;
pageList.forEach(function(page) {
if(c == 0) {
p++;
if(tagPage !== undefined) {
tagPage.metadata.nexturl = file+"/"+p;
list.addPage(tagPage);
}
tagPage = new Page();
tagPage.file = file+"/"+p;
tagPage.type = "tag";
tagPage.metadata.prevurl = prevurl;
tagPage.content = new Array();
prevurl = tagPage.file;
}
tagPage.content.push(page);
c++;
c %= tr.linksPerPage;
});
list.addPage(tagPage);
}
tags.forEach(function(tag) {
generateTagPages("tag/"+tag.url, tagPages[tag.url])
})
cb();
}
}
var Latinise={};Latinise.latin_map={"Á":"A","Ă":"A","":"A","":"A","":"A","":"A","":"A","Ǎ":"A","Â":"A","":"A","":"A","":"A","":"A","":"A","Ä":"A","Ǟ":"A","Ȧ":"A","Ǡ":"A","":"A","Ȁ":"A","À":"A","":"A","Ȃ":"A","Ā":"A","Ą":"A","Å":"A","Ǻ":"A","":"A","Ⱥ":"A","Ã":"A","":"AA","Æ":"AE","Ǽ":"AE","Ǣ":"AE","":"AO","":"AU","":"AV","":"AV","":"AY","":"B","":"B","Ɓ":"B","":"B","Ƀ":"B","Ƃ":"B","Ć":"C","Č":"C","Ç":"C","":"C","Ĉ":"C","Ċ":"C","Ƈ":"C","Ȼ":"C","Ď":"D","":"D","":"D","":"D","":"D","Ɗ":"D","":"D","Dz":"D","Dž":"D","Đ":"D","Ƌ":"D","DZ":"DZ","DŽ":"DZ","É":"E","Ĕ":"E","Ě":"E","Ȩ":"E","":"E","Ê":"E","":"E","":"E","":"E","":"E","":"E","":"E","Ë":"E","Ė":"E","":"E","Ȅ":"E","È":"E","":"E","Ȇ":"E","Ē":"E","":"E","":"E","Ę":"E","Ɇ":"E","":"E","":"E","":"ET","":"F","Ƒ":"F","Ǵ":"G","Ğ":"G","Ǧ":"G","Ģ":"G","Ĝ":"G","Ġ":"G","Ɠ":"G","":"G","Ǥ":"G","":"H","Ȟ":"H","":"H","Ĥ":"H","":"H","":"H","":"H","":"H","Ħ":"H","Í":"I","Ĭ