From c1aa7509008d3aaa4afa9ceb3c3e029b9542f75a Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Thu, 8 Sep 2011 15:53:31 +0200 Subject: Improve title of weblog pages. --- org/madore/damlengine/TodoElement.java | 1 + org/madore/damlengine/TodoTitleElement.java | 55 +++++++++++++++++----- .../damlengine/TodoWeblogSelectionElement.java | 24 ++++++++++ org/madore/damlengine/weblog-cat-template.daml | 2 +- org/madore/damlengine/weblog-index-template.daml | 2 +- org/madore/damlengine/weblog-month-template.daml | 2 +- 6 files changed, 70 insertions(+), 16 deletions(-) (limited to 'org/madore/damlengine') diff --git a/org/madore/damlengine/TodoElement.java b/org/madore/damlengine/TodoElement.java index ee1a90f..12810c0 100644 --- a/org/madore/damlengine/TodoElement.java +++ b/org/madore/damlengine/TodoElement.java @@ -67,6 +67,7 @@ public abstract class TodoElement extends TodoItem { damlFactories.put("weblog-selection-month-year", new TodoWeblogSelectionElement.Factory(TodoWeblogSelectionElement.Type.MONTH_YEAR)); damlFactories.put("weblog-selection-month-month", new TodoWeblogSelectionElement.Factory(TodoWeblogSelectionElement.Type.MONTH_MONTH)); damlFactories.put("weblog-selection-cat-code", new TodoWeblogSelectionElement.Factory(TodoWeblogSelectionElement.Type.CATEGORY_CODE)); + damlFactories.put("weblog-selection-cat-name", new TodoWeblogSelectionElement.Factory(TodoWeblogSelectionElement.Type.CATEGORY_NAME)); } protected final static Factory killAFactory diff --git a/org/madore/damlengine/TodoTitleElement.java b/org/madore/damlengine/TodoTitleElement.java index 877e719..9854f1a 100644 --- a/org/madore/damlengine/TodoTitleElement.java +++ b/org/madore/damlengine/TodoTitleElement.java @@ -20,23 +20,52 @@ public final class TodoTitleElement extends TodoDefaultElement { super(node, ctx, caller); } + public class TodoAgain extends TodoItem { + /* Make this a member class (i.e., not "static") so we can + * access the "node" field of the container class. Another + * option would have been to create a new subclass of + * TodoElement and initialize it on the same node: which is + * better? */ + public TodoAgain(Context ctx, TodoItem caller) { + super(ctx, caller); + } + @Override + public void handle() { + assert(this.ctx == TodoTitleElement.this.ctx); + assert(this.caller == TodoTitleElement.this); + if ( ctx.gc.title != null ) + throw new IllegalArgumentException("attempting to redefine title"); + ctx.gc.title = ctx.doc.createDocumentFragment(); + ctx.gc.titleStr = node.getTextContent(); + ctx.gc.titleLang = LangHelper.getLangRec(node); + String lang = LangHelper.getLangNorec(node); + ArrayList childList = getChildList(node); + for ( Node child : childList ) { + ctx.gc.title.appendChild(child); + } + Element tit = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "title"); + if ( lang != null ) + LangHelper.setLangNorec(tit, lang); + node.getParentNode().replaceChild(tit, node); + tit.appendChild(ctx.doc.createTextNode(ctx.gc.titleStr)); + } + } + @Override public void handleNodeOnly() { - if ( ctx.gc.title != null ) - throw new IllegalArgumentException("attempting to redefine title"); - ctx.gc.title = ctx.doc.createDocumentFragment(); - ctx.gc.titleStr = node.getTextContent(); - ctx.gc.titleLang = LangHelper.getLangRec(node); - String lang = LangHelper.getLangNorec(node); - ArrayList childList = getChildList(node); + // First process the children, then come back to processing + // title, so we can extract the text content after replacement. + ArrayList childList = getChildList(this.node); + ArrayList toProcess = new ArrayList(childList.size()); for ( Node child : childList ) { - ctx.gc.title.appendChild(child); + if ( child.getNodeType() == Node.ELEMENT_NODE ) { + TodoElement it + = TodoElement.getTodoElement((Element)child, this.ctx, this); + toProcess.add(it); + } } - Element tit = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "title"); - if ( lang != null ) - LangHelper.setLangNorec(tit, lang); - node.getParentNode().replaceChild(tit, node); - tit.appendChild(ctx.doc.createTextNode(ctx.gc.titleStr)); + this.ownerDeque.registerAtStart(new TodoAgain(ctx, this)); + this.ownerDeque.registerAtStart(toProcess); } } diff --git a/org/madore/damlengine/TodoWeblogSelectionElement.java b/org/madore/damlengine/TodoWeblogSelectionElement.java index 23c88ec..e33f2c0 100644 --- a/org/madore/damlengine/TodoWeblogSelectionElement.java +++ b/org/madore/damlengine/TodoWeblogSelectionElement.java @@ -1,5 +1,7 @@ package org.madore.damlengine; +import java.util.Map; +import java.util.HashMap; import org.w3c.dom.*; public final class TodoWeblogSelectionElement extends TodoDefaultElement { @@ -8,6 +10,7 @@ public final class TodoWeblogSelectionElement extends TodoDefaultElement { RECENT_COUNT, MONTH_YEAR, MONTH_MONTH, + CATEGORY_NAME, CATEGORY_CODE; } @@ -35,6 +38,22 @@ public final class TodoWeblogSelectionElement extends TodoDefaultElement { this.t = t; } + protected final static Map> categoryNames; + + static { + categoryNames = new HashMap>(); + categoryNames.put("en", new HashMap()); + categoryNames.put("fr", new HashMap()); + categoryNames.get("en").put("glf", "Gratuitous Literary Fragments"); + categoryNames.get("fr").put("glf", "Fragments lit\u00e9raires gratuits"); + } + + public String categoryName(String code, String lang) { + if ( categoryNames.get(lang) == null ) + return code; + return categoryNames.get(lang).get(code); + } + @Override public void handleNodeOnly() { if ( ctx.wsc == null ) @@ -62,6 +81,11 @@ public final class TodoWeblogSelectionElement extends TodoDefaultElement { throw new IllegalStateException("weblog-selection-recent-count element encountered while not in weblog category selection state"); str = ((Context.WeblogCategorySelectionContext)(ctx.wsc)).code; break; + case CATEGORY_NAME: + if ( ! ( ctx.wsc instanceof Context.WeblogCategorySelectionContext ) ) + throw new IllegalStateException("weblog-selection-recent-count element encountered while not in weblog category selection state"); + str = categoryName(((Context.WeblogCategorySelectionContext)(ctx.wsc)).code, LangHelper.getLangRec(node)); + break; default: throw new AssertionError("unknown type"); } diff --git a/org/madore/damlengine/weblog-cat-template.daml b/org/madore/damlengine/weblog-cat-template.daml index 01a56a0..7219ea3 100644 --- a/org/madore/damlengine/weblog-cat-template.daml +++ b/org/madore/damlengine/weblog-cat-template.daml @@ -14,7 +14,7 @@ -David Madore's WebLog +David Madore's WebLog: David Alexander Madore's WebLog / Diary diff --git a/org/madore/damlengine/weblog-index-template.daml b/org/madore/damlengine/weblog-index-template.daml index f032b7b..3cd1277 100644 --- a/org/madore/damlengine/weblog-index-template.daml +++ b/org/madore/damlengine/weblog-index-template.daml @@ -14,7 +14,7 @@ -David Madore's WebLog +David Madore's WebLog: index David Alexander Madore's WebLog / Diary diff --git a/org/madore/damlengine/weblog-month-template.daml b/org/madore/damlengine/weblog-month-template.daml index 69a28b4..913f756 100644 --- a/org/madore/damlengine/weblog-month-template.daml +++ b/org/madore/damlengine/weblog-month-template.daml @@ -14,7 +14,7 @@ -David Madore's WebLog +David Madore's WebLog: - David Alexander Madore's WebLog / Diary -- cgit v1.2.3