Skip to content

Commit

Permalink
Issue #2: categories are now represented as top level menus, with eac…
Browse files Browse the repository at this point in the history
…h level having it's parent properly set.
  • Loading branch information
davetcc committed Oct 29, 2016
1 parent 90ff280 commit 6e0e620
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ public class JoomlaContent {
private final String title;
private final String alias;
private final int status;
private final String parent;
private JoomlaImage introImage = JoomlaImage.EMPTY;
private JoomlaImage bodyImage = JoomlaImage.EMPTY;

public JoomlaContent(int id, int status, String author, LocalDate createdDate, String intro,
String body, String category, String title, String alias,
String images) {
String images, String parent) {
this.id = id;
this.status = status;
this.author = author;
Expand All @@ -38,6 +39,7 @@ public JoomlaContent(int id, int status, String author, LocalDate createdDate, S
this.category = category;
this.title = title;
this.alias = alias;
this.parent = parent;
try {
introImage = new JoomlaImage("intro", images);
bodyImage = new JoomlaImage("fulltext", images);
Expand Down Expand Up @@ -107,4 +109,8 @@ public JoomlaImage getBodyImage() {
public boolean isPublished() {
return status == 0 || status == 1;
}

public String getParent() {
return parent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class JoomlaHugoConverter {
private final NastyContentChecker nastyContentChecker;


private final Template tomlTemplate;
private final Template contentTemplate;
private final Template categoryTemplate;
private final Multimap<Integer, String> tagsByName = LinkedListMultimap.create(100);
private final String dbExtension;

Expand All @@ -48,7 +49,8 @@ public JoomlaHugoConverter(NastyContentChecker nastyContentChecker,
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);

tomlTemplate = cfg.getTemplate("defaultPage.toml.ftl");
categoryTemplate = cfg.getTemplate("categoryPage.toml.ftl");
contentTemplate = cfg.getTemplate("defaultPage.toml.ftl");

buildTags();
}
Expand Down Expand Up @@ -76,7 +78,7 @@ public void performConversion() {
String articleQuery =
"select C.id as id, U.username as username, C.created as created, C.introtext as intro, " +
" C.`fulltext` as full, D.path as path, C.title as title, C.alias as alias,\n" +
" C.images as images, c.state as state \n" +
" C.images as images, c.state as state, D.alias as catAlias \n" +
"from REPLSTR_content C, REPLSTR_users U, REPLSTR_categories D\n" +
"where C.created_by = U.id\n" +
" and D.id = C.catid\n" +
Expand All @@ -93,10 +95,18 @@ public void performConversion() {
resultSet.getString("path"),
resultSet.getString("title"),
resultSet.getString("alias"),
resultSet.getString("images")
resultSet.getString("images"),
resultSet.getString("catAlias")
));

iterateOverContentList(content);
content.stream().filter(JoomlaContent::isPublished).forEach(c-> {
nastyContentChecker.checkForNastyContent(c);
Path path = Paths.get(pathToOutput);
logger.info("processing {} {} {}", c.getTitle(), c.getCategory(), c.getAlias());
Path newPath = path.resolve(c.getCategory());
newPath.toFile().mkdirs();
buildTomlOutput(c, newPath.resolve(c.getId() + "-" + c.getAlias() + ".md"), contentTemplate);
});

content.stream().filter(c-> !c.isPublished()).forEach(
c->logger.info("Skipping deleted content {} {}", c.getTitle(), c.getId())
Expand All @@ -111,19 +121,13 @@ public void performConversion() {
}
}

private void iterateOverContentList(List<JoomlaContent> content) {
content.stream().filter(JoomlaContent::isPublished).forEach(c-> {
nastyContentChecker.checkForNastyContent(c);
Path path = Paths.get(pathToOutput);
logger.info("processing {} {}", c.getTitle(), c.getCategory(), c.getAlias());
Path newPath = path.resolve(c.getCategory());
newPath.toFile().mkdirs();
buildTomlOutput(c, newPath.resolve(c.getId() + "-" + c.getAlias() + ".md"));
});
}

private void performCategoryConversion() {
String sqlCat = "select id, alias, title, description, path, created_time, published, description from REPLSTR_categories where length(description) > 0";
String sqlCat =
"SELECT C.id AS id, C.alias AS alias, C.description AS description, C.path AS path,\n" +
" C.created_time AS created_time, C.published AS published, C.description description, C.title AS title,\n" +
" P.alias AS parent\n" +
"FROM REPLSTR_categories C, REPLSTR_categories P\n" +
"WHERE C.parent_id = P.id\n";
sqlCat = sqlCat.replace("REPLSTR", dbExtension);
List<JoomlaContent> content = template.query(sqlCat, (resultSet, i) -> new JoomlaContent(
resultSet.getInt("id"),
Expand All @@ -132,17 +136,27 @@ private void performCategoryConversion() {
resultSet.getDate("created_time").toLocalDate(),
resultSet.getString("title"),
resultSet.getString("description"),
resultSet.getString("path"),
resultSet.getString("parent"),
resultSet.getString("title"),
resultSet.getString("alias") + "-category",
"{}"
resultSet.getString("alias"),
"{}",
resultSet.getString("path")

));
iterateOverContentList(content);

content.stream().filter(JoomlaContent::isPublished).forEach(c-> {
nastyContentChecker.checkForNastyContent(c);
Path path = Paths.get(pathToOutput);
logger.info("processing category {} {} {}", c.getTitle(), c.getCategory(), c.getAlias());
Path newPath = path.resolve(c.getParent() + ".md");
buildTomlOutput(c, newPath, categoryTemplate);
});

logger.info("Category description creation complete");
}


public void buildTomlOutput(JoomlaContent content, Path resolve) {
public void buildTomlOutput(JoomlaContent content, Path resolve, Template template) {

try {
String tagsQuoted = tagsByName.get(content.getId()).stream()
Expand All @@ -153,7 +167,7 @@ public void buildTomlOutput(JoomlaContent content, Path resolve) {
root.put("joomlaData", content);
root.put("tags", tagsQuoted);
root.put("body", urlSorter(content.getIntro() + "\n" + content.getBody()));
tomlTemplate.process(root, new BufferedWriter(new FileWriter(resolve.toFile())));
template.process(root, new BufferedWriter(new FileWriter(resolve.toFile())));
} catch (Exception e) {
logger.error("Failed to generate file", e);
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/categoryPage.toml.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<#-- @ftlvariable name="joomlaData" type="com.thecoderscorner.web.hugojoomla.JoomlaContent" -->
+++
title = "${joomlaData.title}"
description = "${joomlaData.introAsSingleLine}"
date = "${joomlaData.createdDateAsText}"
author = "${joomlaData.author}"
showChildren = true
type = "category"

[menu.main]
name = "${joomlaData.title}"
identifier = "${joomlaData.alias}"
parent = "${joomlaData.category}"
+++

${body}
4 changes: 3 additions & 1 deletion src/main/resources/defaultPage.toml.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ description = "${joomlaData.introAsSingleLine}"
tags = [ ${tags} ]
type = "blog"
date = "${joomlaData.createdDateAsText}"
categories = ["${joomlaData.category}"]
author = "${joomlaData.author}"
<#if joomlaData.introImage.isImagePresent()>
banner = "${joomlaData.introImage.url}"
</#if>
[menu.main]
name = "${joomlaData.title}"
parent = "${joomlaData.parent}"
+++
<#if joomlaData.bodyImage.isImagePresent() >
<img class="${joomlaData.bodyImage.htmlClass} titleimg" alt="${joomlaData.bodyImage.alt}" src="${joomlaData.bodyImage.url}"/>
Expand Down

0 comments on commit 6e0e620

Please sign in to comment.