Commit 82b133be authored by Isabella Skořepová's avatar Isabella Skořepová

Generate redirects from content/redirects

parent 314f90d1
Pipeline #151 passed with stages
......@@ -39,6 +39,7 @@ module.exports = function() {
staticDir: options.staticdir,
themeDir: options.themedir,
articlesLocation: 'articles',
redirectsLocation: 'redirects',
linksPerPage: 6
}});
}
......
'use strict';
var toml = require('toml');
var path = require('path');
var readRedirectWorker = function(content, obj) {
if(content === undefined) throw new Error('content is undefined');
try {
obj.metadata = toml.parse(content);
} catch(e) {
console.log(obj.filename+': Failed to parse metadata');
throw e;
}
obj.content = obj.metadata.target;
console.log(obj.file)
obj.file = obj.file.split(path.sep).join('/');
if(obj.file.match(/^redirects\/(.*)index\.toml/))
obj.file = obj.file.replace(/^redirects\/(.*)index\.toml/,'$1');
else
obj.file = obj.file.replace(/^redirects\/(.*)\.toml/,'$1');
console.log(obj.file)
return obj;
}
module.exports = function(obj) {
console.log('Build step: ParseRedirects');
return new Promise(function(resolve, reject) {
var redirects = [];
obj.redirects.forEach(function(page) {
redirects.push(readRedirectWorker(page.content, page));
})
obj.redirects = redirects;
resolve(obj);
});
}
......@@ -8,6 +8,7 @@ module.exports = function(jobs) {
'config',
'readFiles',
'parseHugo',
'parseRedirects',
'gitInfo',
'urls',
'sitemap',
......
'use strict';
/*
* This job reads files from config.articlesLocation and loads this list to `pages`
*/
......@@ -7,33 +8,57 @@ var path = require('path');
module.exports = function(obj) {
console.log('Build step: ReadFiles');
return new Promise(function(resolve, reject) {
var onFile = function(root, fileStats, next, array) {
var filename = path.join(root,fileStats.name);
var file = path.relative(obj.config.sourceDir, filename);
fs.readFile(filename, 'utf8', function(err,data) {
if(err) {
console.log('Error reading file '+filename)
console.log(err);
} else {
array.push({
filename: filename,
file: file,
content: data
})
}
next()
})
};
var promise1 = new Promise(function(resolve, reject) {
var pages = [];
walk.walk(path.join(obj.config.sourceDir,obj.config.articlesLocation))
.on('file',function(root, fileStats, next) {
var filename = path.join(root,fileStats.name);
var file = path.relative(obj.config.sourceDir, filename);
fs.readFile(filename, 'utf8', function(err,data) {
if(err) {
console.log('Error reading file '+filename)
console.log(err);
} else {
pages.push({
filename: filename,
file: file,
content: data
})
}
next()
})
.on('file',function(root,fileStats,next) {
onFile(root,fileStats,next,pages);
})
.on('errors',function(root, nodeStatsArray, next) {
console.log('Walker error', root, nodeStatsArray);
next();
})
.on('end', function() {
resolve({type: 'pages', val: pages});
})
})
var promise2 = new Promise(function(resolve, reject) {
let redirects = [];
walk.walk(path.join(obj.config.sourceDir,obj.config.redirectsLocation))
.on('file',function(root,fileStats,next) {
onFile(root,fileStats,next,redirects);
})
.on('errors',function(root, nodeStatsArray, next) {
console.log('Walker error', root, nodeStatsArray);
next();
})
.on('end', function() {
obj.pages = pages;
resolve(obj);
resolve({type: 'redirects', val: redirects});
})
})
return Promise.all([promise1,promise2]).then(function(arr) {
arr.forEach(function(v) {
obj[v.type] = v.val;
});
return obj;
});
}
......@@ -17,6 +17,7 @@ module.exports = function(config) {
['writeFiles', './writeFiles'],
['resetJobs', './resetJobs'],
['sitemap', './sitemap'],
['parseRedirects','./parseRedirects'],
['pipeline','./pipeline']
)
......
......@@ -42,7 +42,7 @@ module.exports = function(obj) {
});
obj.tags = [];
obj.redirects = [];
if(!Array.isArray(obj.redirects)) obj.redirects = [];
var generateTagPages = function(file, pageList) {
var pg = {};
......
......@@ -26,6 +26,8 @@ module.exports = function(obj) {
var f = path.join(builddir,article.file,'index.'+o.type);
doWrite(f, o.content);
})
} else {
console.log(article);
}
}
obj.pages.forEach(function(o) { writeFile(o); })
......
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="1;url=/tag/clanek">
<script type="text/javascript">
window.location.href = "/tag/clanek"
</script>
<title>Přesměrování...</title>
</head>
<body>
Pokud nebudete přesměrováni automaticky tak následujte <a href='/tag/clanek'>tento odkaz</a>
</body>
</html>
<?php
header("location: /tag/clanek");
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="1;url=/tag/clanek">
<script type="text/javascript">
window.location.href = "/tag/clanek"
</script>
<title>Přesměrování...</title>
</head>
<body>
Pokud nebudete přesměrováni automaticky tak následujte <a href='/tag/clanek'>tento odkaz</a>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment