Skip to content

Commit

Permalink
#102 preview mode (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
thmarx authored Dec 1, 2023
1 parent 778dd97 commit e8ed5ac
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github.thmarx.cms.api;

/*-
* #%L
* cms-api
* %%
* 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%
*/

/**
*
* @author t.marx
*/
public class PreviewContext {

public static ThreadLocal<Boolean> IS_PREVIEW = ThreadLocal.withInitial(() -> false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import com.github.thmarx.cms.api.Constants;
import com.github.thmarx.cms.api.PreviewContext;
import com.github.thmarx.cms.api.utils.SectionUtil;
import com.github.thmarx.cms.filesystem.datafilter.DataFilter;
import com.google.common.base.Strings;
Expand Down Expand Up @@ -95,19 +96,7 @@ public List<MetaNode> listChildren(String uri) {
if ("".equals(uri)) {
return tree.values().stream()
.filter(node -> !node.isHidden())
.map(node -> {
if (node.isDirectory) {
var tempNode = node.children.entrySet().stream().filter((entry)
-> entry.getKey().equals("index.md")
).findFirst();
if (tempNode.isPresent()) {
return tempNode.get().getValue();
}
return null;
} else {
return node;
}
})
.map(this::mapToIndex)
.filter(node -> node != null)
.filter(MetaData::isVisible)
.collect(Collectors.toList());
Expand All @@ -118,19 +107,7 @@ public List<MetaNode> listChildren(String uri) {
return findFolder.get().children().values()
.stream()
.filter(node -> !node.isHidden())
.map(node -> {
if (node.isDirectory) {
var tempNode = node.children.entrySet().stream().filter((entry)
-> entry.getKey().equals("index.md")
).findFirst();
if (tempNode.isPresent()) {
return tempNode.get().getValue();
}
return null;
} else {
return node;
}
})
.map(this::mapToIndex)
.filter(node -> node != null)
.filter(MetaData::isVisible)
.collect(Collectors.toList());
Expand All @@ -139,6 +116,20 @@ public List<MetaNode> listChildren(String uri) {
return Collections.emptyList();
}

protected MetaNode mapToIndex(MetaNode node) {
if (node.isDirectory) {
var tempNode = node.children.entrySet().stream().filter((entry)
-> entry.getKey().equals("index.md")
).findFirst();
if (tempNode.isPresent()) {
return tempNode.get().getValue();
}
return null;
} else {
return node;
}
}

public static boolean isVisible (MetaNode node) {
return node != null
&& node.isPublished()
Expand Down Expand Up @@ -222,6 +213,9 @@ public boolean isDraft() {
}

public boolean isPublished() {
if (PreviewContext.IS_PREVIEW.get()) {
return true;
}
var localDate = (Date) data.getOrDefault(Constants.MetaFields.PUBLISHED, Date.from(Instant.now()));
var now = Date.from(Instant.now());
return !isDraft() && (localDate.before(now) || localDate.equals(now));
Expand Down
1 change: 1 addition & 0 deletions cms-server/hosts/demo/content/templating/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: Templates & Engines
template: content.html
draft: true
menu:
title: Templating
position: 20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ public Optional<String> getStaticContent (String uri) {
if (!PathUtil.isChild(contentBase, staticFile)) {
return Optional.empty();
}
return Optional.ofNullable(Files.readString(staticFile, StandardCharsets.UTF_8));
if (Files.exists(staticFile)) {
return Optional.ofNullable(Files.readString(staticFile, StandardCharsets.UTF_8));
}
} catch (IOException ex) {
log.error("", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* #L%
*/

import com.github.thmarx.cms.Startup;
import com.github.thmarx.cms.api.markdown.MarkdownRenderer;
import com.github.thmarx.cms.api.theme.Theme;
import com.github.thmarx.cms.content.ContentTags;
Expand All @@ -45,7 +46,7 @@ public class RequestContextFactory {


public RequestContext create (String uri, Map<String, List<String>> queryParameters) throws IOException {

var requestTheme = new RequestTheme(theme);

RequestExtensions requestExtensions = extensionManager.newContext(requestTheme);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.github.thmarx.cms.Startup;
import com.github.thmarx.cms.api.PreviewContext;
import com.github.thmarx.cms.content.ContentResolver;
import com.github.thmarx.cms.request.RequestContext;
import com.github.thmarx.cms.request.RequestContextFactory;
import com.github.thmarx.cms.utils.HTTPUtil;
import java.util.Optional;
Expand Down Expand Up @@ -50,6 +53,10 @@ public boolean handle(Request request, Response response, Callback callback) thr
var queryParameters = HTTPUtil.queryParameters(request.getHttpURI().getQuery());
try (
var requestContext = requestContextFactory.create(uri, queryParameters)) {

if (Startup.DEV_MODE && queryParameters.containsKey("preview")) {
PreviewContext.IS_PREVIEW.set(Boolean.TRUE);
}

Optional<String> content = contentResolver.getContent(requestContext);
response.setStatus(200);
Expand All @@ -74,6 +81,8 @@ public boolean handle(Request request, Response response, Callback callback) thr
response.setStatus(500);
response.getHeaders().add("Content-Type", "text/html; charset=utf-8");
callback.succeeded();
} finally {
PreviewContext.IS_PREVIEW.remove();
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* #L%
*/

import com.github.thmarx.cms.api.PreviewContext;
import com.github.thmarx.cms.content.ContentParser;
import com.github.thmarx.cms.filesystem.FileSystem;
import com.github.thmarx.cms.api.markdown.MarkdownRenderer;
Expand Down Expand Up @@ -63,7 +64,9 @@ protected String getUrl(Path node) {

var url = sb.toString();

return "".equals(url) ? "/" : url;
url = "".equals(url) ? "/" : url;

return url + (PreviewContext.IS_PREVIEW.get() ? "?preview" : "");
}

protected Optional<ContentParser.Content> parse(Path node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.github.thmarx.cms.api.PreviewContext;
import com.github.thmarx.cms.api.markdown.MarkdownRenderer;
import com.github.thmarx.cms.content.ContentParser;
import com.github.thmarx.cms.filesystem.FileSystem;
Expand All @@ -31,7 +32,6 @@
import com.github.thmarx.cms.utils.NodeUtil;
import java.nio.file.Path;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
*
Expand Down Expand Up @@ -85,6 +85,6 @@ protected String toUrl(String uri) {
uri = uri.substring(0, uri.length() - 1);
}

return uri;
return uri + (PreviewContext.IS_PREVIEW.get() ? "?preview" : "");
}
}

0 comments on commit e8ed5ac

Please sign in to comment.