Commits (3)
......@@ -17,7 +17,17 @@ void main()
if(res.statusCode != 200)
return;
auto data = res.readJson();
logInfo("%s", meArticles(data));
auto savePath = "devArticles";
import articlewriter;
import std.file;
import std.path;
if(!exists(buildPath(savePath, articleContentFolder)))
mkdirRecurse(buildPath(savePath, articleContentFolder));
meArticles(data)
.structureArticles
.saveArticles(savePath);
}
);
}
......
......@@ -18,6 +18,7 @@ struct ArticleMeta {
@optional string series;
@optional string type_of;
@optional string[] tags;
@optional string content_file;
this(ArticleMe me) {
canonical_url = me.canonical_url;
......@@ -45,6 +46,8 @@ struct ArticleMeta {
}
}
enum articleContentFolder = "Articles";
alias Article = Tuple!(ArticleMeta, "meta", string, "content");
auto structureArticles(Range)(Range articles) if (isInputRange!Range
......@@ -85,18 +88,47 @@ auto structureArticles(Range)(Range articles) if (isInputRange!Range
assert(structureArticles(exampleResponse).equal([Article(ans, "Content Body")]));
}
private struct Paths {
string articleSaveLocation;
string metaSaveLocation;
string articleMetaPath;
}
private auto articlePaths(string folder, ArticleMeta am)
out(path) {
assert(path.articleSaveLocation != path.metaSaveLocation, "Can't save to same file");
assert(path.articleSaveLocation.endsWith(path.articleMetaPath), "Meta Reference to Article File must match.");
assert(!path.articleSaveLocation.find(folder).empty, "Save Locations should utilized specified folder");
assert(!path.metaSaveLocation.find(folder).empty, "Save Locations should utilized specified folder");
} do {
Paths ret;
auto filename = am.title;
assert(isValidFilename(filename), "Article Name Conflicts with valid file name.");
ret.articleMetaPath = buildPath(articleContentFolder, filename.setExtension("md"));
ret.articleSaveLocation = folder.buildPath(ret.articleMetaPath);
ret.metaSaveLocation = folder.buildPath(filename.setExtension("json"));
return ret;
} unittest {
ArticleMeta meta;
meta.title = "Article Title";
articlePaths("someLocation", meta);
}
void saveArticles(Range)(Range articles, string folder) if(is(ElementType!Range == Article))
in(folder.exists, "Create direcorty before calling to save articles.")
in(buildPath(folder,articleContentFolder).exists, format("Create direcorty `buildPath(\"%s\",articleContentFolder)` before calling to save articles.", folder))
{
void saveArticle(Article article) {
import std.file;
import std.path;
auto filename = article.meta.title;
assert(isValidFilename(filename), "Article Name Conflicts with valid file name.");
auto paths = articlePaths(folder, article.meta);
write(folder.buildPath(filename.setExtension("md")), article.content);
write(folder.buildPath(filename.setExtension("json")), article.meta.serializeToPrettyJson);
article.meta.content_file = paths.articleMetaPath;
write(paths.articleSaveLocation, article.content);
write(paths.metaSaveLocation, article.meta.serializeToPrettyJson);
}
articles.each!saveArticle;
......