Skip to content

Commit

Permalink
#98 sitemap generator added
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Dec 3, 2023
1 parent 3d2c922 commit b1ea159
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.github.thmarx.cms.api.PreviewContext;
import com.github.thmarx.cms.api.utils.SectionUtil;
import java.time.Instant;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ protected ContentNode mapToIndex(ContentNode node) {

public static boolean isVisible (ContentNode node) {
return node != null
// check if some parent is hidden
&& !node.uri().startsWith(".") && !node.uri().contains("/.")
&& node.isPublished()
&& !node.isHidden()
&& !node.isSection();
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"modules":{"flexmark-module":{"active":false,"id":"flexmark-module","moduleDir":"flexmark-module"},"thymeleaf-module":{"active":true,"id":"thymeleaf-module","moduleDir":"thymeleaf-module"},"freemarker-module":{"active":false,"id":"freemarker-module","moduleDir":"freemarker-module"},"pug-module":{"active":false,"id":"pug-module","moduleDir":"pug-module"},"search-module":{"active":true,"id":"search-module","moduleDir":"search-module"},"pebble-module":{"active":false,"id":"pebble-module","moduleDir":"pebble-module"},"example-module":{"active":true,"id":"example-module","moduleDir":"example-module"},"markedjs-module":{"active":true,"id":"markedjs-module","moduleDir":"markedjs-module"}}}
{"modules":{"flexmark-module":{"active":false,"id":"flexmark-module","moduleDir":"flexmark-module"},"thymeleaf-module":{"active":true,"id":"thymeleaf-module","moduleDir":"thymeleaf-module"},"freemarker-module":{"active":false,"id":"freemarker-module","moduleDir":"freemarker-module"},"pug-module":{"active":false,"id":"pug-module","moduleDir":"pug-module"},"search-module":{"active":true,"id":"search-module","moduleDir":"search-module"},"pebble-module":{"active":false,"id":"pebble-module","moduleDir":"pebble-module"},"seo-module":{"active":true,"id":"seo-module","moduleDir":"seo-module"},"example-module":{"active":true,"id":"example-module","moduleDir":"example-module"},"markedjs-module":{"active":true,"id":"markedjs-module","moduleDir":"markedjs-module"}}}
Empty file.
1 change: 1 addition & 0 deletions cms-server/hosts/demo/site.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ theme: default
modules:
active:
- markedjs-module
- seo-module
media:
formats:
- name: small
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"modules":{"flexmark-module":{"active":false,"id":"flexmark-module","moduleDir":"flexmark-module"},"thymeleaf-module":{"active":false,"id":"thymeleaf-module","moduleDir":"thymeleaf-module"},"freemarker-module":{"active":false,"id":"freemarker-module","moduleDir":"freemarker-module"},"pug-module":{"active":false,"id":"pug-module","moduleDir":"pug-module"},"search-module":{"active":false,"id":"search-module","moduleDir":"search-module"},"pebble-module":{"active":true,"id":"pebble-module","moduleDir":"pebble-module"},"example-module":{"active":false,"id":"example-module","moduleDir":"example-module"},"markedjs-module":{"active":true,"id":"markedjs-module","moduleDir":"markedjs-module"}}}
{"modules":{"flexmark-module":{"active":false,"id":"flexmark-module","moduleDir":"flexmark-module"},"thymeleaf-module":{"active":false,"id":"thymeleaf-module","moduleDir":"thymeleaf-module"},"freemarker-module":{"active":false,"id":"freemarker-module","moduleDir":"freemarker-module"},"pug-module":{"active":false,"id":"pug-module","moduleDir":"pug-module"},"search-module":{"active":false,"id":"search-module","moduleDir":"search-module"},"pebble-module":{"active":true,"id":"pebble-module","moduleDir":"pebble-module"},"seo-module":{"active":false,"id":"seo-module","moduleDir":"seo-module"},"example-module":{"active":false,"id":"example-module","moduleDir":"example-module"},"markedjs-module":{"active":true,"id":"markedjs-module","moduleDir":"markedjs-module"}}}
Binary file modified cms-server/modules/example-module/libs/example-module-2.14.0.jar
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions cms-server/modules/seo-module/module.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id=seo-module
name=seo module
version=2.14.0
priority=HIGH
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.github.thmarx.cms.modules.example;

/*-
* #%L
* seo-module
* %%
* Copyright (C) 2023 Marx-Software
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/

import com.github.thmarx.cms.api.SiteProperties;
import com.github.thmarx.cms.api.db.ContentNode;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import lombok.RequiredArgsConstructor;

/**
*
* @author t.marx
*/
@RequiredArgsConstructor
public class SitemapGenerator implements AutoCloseable {

private final OutputStream output;
private final SiteProperties siteProperties;


public void start () throws IOException {
output.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>".getBytes(StandardCharsets.UTF_8));
output.write("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">".getBytes(StandardCharsets.UTF_8));
}

public void addNode (final ContentNode node) throws IOException {
output.write("<url>".getBytes(StandardCharsets.UTF_8));
output.write("<loc>%s/%s</loc>".formatted(
siteProperties.getOrDefault("baseurl", ""),
node.uri()
).getBytes(StandardCharsets.UTF_8));
output.write("<lastmod></lastmod>".getBytes(StandardCharsets.UTF_8));
output.write("</url>".getBytes(StandardCharsets.UTF_8));
}

@Override
public void close() throws Exception {
output.write("</urlset>".getBytes(StandardCharsets.UTF_8));
output.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@
* #L%
*/

import com.github.thmarx.cms.api.CMSModuleContext;
import com.github.thmarx.cms.api.extensions.JettyHttpHandlerExtensionPoint;
import com.github.thmarx.cms.api.extensions.Mapping;
import com.github.thmarx.modules.api.annotation.Extension;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.pathmap.PathSpec;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
Expand All @@ -38,24 +44,43 @@
*
* @author t.marx
*/
@Slf4j
@Extension(JettyHttpHandlerExtensionPoint.class)
public class ExampleJettyHttpHandlerExtension extends JettyHttpHandlerExtensionPoint {
public class SitemapJettyHttpHandlerExtension extends JettyHttpHandlerExtensionPoint {

@Override
public Mapping getMapping() {
Mapping mapping = new Mapping();
mapping.add(PathSpec.from("/sitemap.xml"), new ExampleHandler("Hello world!"));
mapping.add(PathSpec.from("/sitemap.xml"), new SitemapHandler(getContext()));
return mapping;
}

@RequiredArgsConstructor
public static class ExampleHandler extends Handler.Abstract {
public static class SitemapHandler extends Handler.Abstract {

private final String message;
private final CMSModuleContext context;

@Override
public boolean handle(Request request, Response response, Callback callback) throws Exception {
response.write(true, ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8)), callback);

try (var sitemap = new SitemapGenerator(
Response.asBufferedOutputStream(request, response),
context.getSiteProperties()
)) {
response.getHeaders().add(HttpHeader.CONTENT_TYPE, "application/xml");
sitemap.start();
context.getDb().getContent().query((node, length) -> node).get().forEach(node -> {
try {
sitemap.addNode(node);
} catch (IOException ex) {
log.error(null, ex);
}
});
} catch (Exception e) {
log.error(null, e);
}
callback.succeeded();

return true;
}

Expand Down

0 comments on commit b1ea159

Please sign in to comment.