From 6e0e6200e52bc1a02bc0bbc3cd51541839a2bb42 Mon Sep 17 00:00:00 2001 From: Dave Cherry Date: Sat, 29 Oct 2016 10:18:49 +0100 Subject: [PATCH] Issue #2: categories are now represented as top level menus, with each level having it's parent properly set. --- .../web/hugojoomla/JoomlaContent.java | 8 ++- .../web/hugojoomla/JoomlaHugoConverter.java | 60 ++++++++++++------- src/main/resources/categoryPage.toml.ftl | 16 +++++ src/main/resources/defaultPage.toml.ftl | 4 +- 4 files changed, 63 insertions(+), 25 deletions(-) create mode 100644 src/main/resources/categoryPage.toml.ftl diff --git a/src/main/java/com/thecoderscorner/web/hugojoomla/JoomlaContent.java b/src/main/java/com/thecoderscorner/web/hugojoomla/JoomlaContent.java index f35c70e..2a80053 100644 --- a/src/main/java/com/thecoderscorner/web/hugojoomla/JoomlaContent.java +++ b/src/main/java/com/thecoderscorner/web/hugojoomla/JoomlaContent.java @@ -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; @@ -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); @@ -107,4 +109,8 @@ public JoomlaImage getBodyImage() { public boolean isPublished() { return status == 0 || status == 1; } + + public String getParent() { + return parent; + } } diff --git a/src/main/java/com/thecoderscorner/web/hugojoomla/JoomlaHugoConverter.java b/src/main/java/com/thecoderscorner/web/hugojoomla/JoomlaHugoConverter.java index 560c677..b243a46 100644 --- a/src/main/java/com/thecoderscorner/web/hugojoomla/JoomlaHugoConverter.java +++ b/src/main/java/com/thecoderscorner/web/hugojoomla/JoomlaHugoConverter.java @@ -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 tagsByName = LinkedListMultimap.create(100); private final String dbExtension; @@ -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(); } @@ -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" + @@ -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()) @@ -111,19 +121,13 @@ public void performConversion() { } } - private void iterateOverContentList(List 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 content = template.query(sqlCat, (resultSet, i) -> new JoomlaContent( resultSet.getInt("id"), @@ -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() @@ -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); } diff --git a/src/main/resources/categoryPage.toml.ftl b/src/main/resources/categoryPage.toml.ftl new file mode 100644 index 0000000..6013abf --- /dev/null +++ b/src/main/resources/categoryPage.toml.ftl @@ -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} \ No newline at end of file diff --git a/src/main/resources/defaultPage.toml.ftl b/src/main/resources/defaultPage.toml.ftl index c304d78..b111c1d 100644 --- a/src/main/resources/defaultPage.toml.ftl +++ b/src/main/resources/defaultPage.toml.ftl @@ -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}" +[menu.main] +name = "${joomlaData.title}" +parent = "${joomlaData.parent}" +++ <#if joomlaData.bodyImage.isImagePresent() > ${joomlaData.bodyImage.alt}