From ffe182b338e90ecd3f702c7142c4e43996e37d3c Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Fri, 26 Aug 2011 16:43:40 +0200 Subject: Implement comments (talkback) link. --- org/madore/damlengine/Context.java | 31 +++++++++++++++- org/madore/damlengine/TodoComments.java | 57 +++++++++++++++++++++++++++++ org/madore/damlengine/TodoEntryElement.java | 11 ++++-- org/madore/damlengine/TodoWrefAttr.java | 2 +- 4 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 org/madore/damlengine/TodoComments.java diff --git a/org/madore/damlengine/Context.java b/org/madore/damlengine/Context.java index 6321ca2..22912a7 100644 --- a/org/madore/damlengine/Context.java +++ b/org/madore/damlengine/Context.java @@ -3,9 +3,9 @@ package org.madore.damlengine; import java.util.ArrayList; import org.w3c.dom.*; -public class Context { +public class Context implements Cloneable { - public Document doc; + public final Document doc; public Element htmlNode; public Element headNode; public String uriToTop; @@ -20,8 +20,35 @@ public class Context { public String subtitleLang; public ArrayList translations; + public static class EntryContext { + public String year; + public String month; + public String day; + public String yandm; + public String date; + public String number; + public String dow; + public EntryContext(String year, String month, String day, + String number, String dow) { + this.year = year; this.month = month; this.day = day; + this.yandm = year+"-"+month; + this.date = yandm+"-"+day; + this.number = number; this.dow = dow; + } + } + + public EntryContext entryCtx; + public Context(Document doc) { this.doc = doc; } + public Context clone() { + try { + return (Context) super.clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeException(e); + } + } + } diff --git a/org/madore/damlengine/TodoComments.java b/org/madore/damlengine/TodoComments.java new file mode 100644 index 0000000..e1dd027 --- /dev/null +++ b/org/madore/damlengine/TodoComments.java @@ -0,0 +1,57 @@ +package org.madore.damlengine; + +import java.util.Map; +import java.util.HashMap; +import org.w3c.dom.*; + +public final class TodoComments extends TodoElement { + + public TodoComments(Element node, + Context ctx, + TodoItem caller) { + super(node, ctx, caller); + } + + protected final static Map linkNameTable; + + static { + linkNameTable = new HashMap(); + linkNameTable.put("en", "Comments"); + linkNameTable.put("fr", "Commentaires"); + linkNameTable.put("de", "Kommentare"); + linkNameTable.put("ia", "Commentos"); + } + + @Override + public void handle() { + Element p = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "p"); + String lang = LangHelper.getLangRec(node); + String explicitLang = LangHelper.getLangNorec(node); + if ( lang == null || linkNameTable.get(lang) == null ) { + System.err.println("warning: will use English comments link"); + lang = "en"; + explicitLang = "en"; + } + if ( explicitLang != null ) + LangHelper.setLangNorec(p, explicitLang); + p.setAttributeNS(null, "class", "talkback-link"); + node.getParentNode().replaceChild(p, node); + Element a = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "a"); + if ( ctx.entryCtx == null ) + throw new IllegalStateException("entry context not defined in comments element"); + String commentURL = "http://www.madore.org/cgi-bin/comment.pl/" + + "showcomments?href=http%3a%2f%2fwww.madore.org%2f" + + "%7edavid%2fweblog%2f" + + ctx.entryCtx.yandm + ".html%23d." + ctx.entryCtx.date + + "." + ctx.entryCtx.number; + a.setAttributeNS(null, "href", commentURL); + p.appendChild(a); + a.appendChild(ctx.doc.createTextNode(linkNameTable.get(lang))); + Element span = ctx.doc.createElementNS(DamlEngine.XHTML_NS, "span"); + span.setAttributeNS(null, "id", + "d." + ctx.entryCtx.date + "." + + ctx.entryCtx.number + ".numcomments"); + p.appendChild(span); + span.appendChild(ctx.doc.createComment(" EMPTY ")); + } +} diff --git a/org/madore/damlengine/TodoEntryElement.java b/org/madore/damlengine/TodoEntryElement.java index 7e6f384..14560a5 100644 --- a/org/madore/damlengine/TodoEntryElement.java +++ b/org/madore/damlengine/TodoEntryElement.java @@ -42,7 +42,7 @@ public final class TodoEntryElement extends TodoDefaultElement { throw new IllegalArgumentException("title date attribute must be of the form YYYY-MM-DD"); String entryYearStr = entryDateMatcher.group(1); String entryMonthStr = entryDateMatcher.group(2); - // String entryDayStr = entryDateMatcher.group(3); + String entryDayStr = entryDateMatcher.group(3); String entryDowStr = node.getAttributeNS(null, "day_of_week"); String entryIdStr = "d."+entryDateStr+"."+entryNumberStr; @@ -69,6 +69,11 @@ public final class TodoEntryElement extends TodoDefaultElement { if ( ! entryDowStr.equals("") ) header.appendChild(ctx.doc.createTextNode(" ("+entryDowStr+")")); + Context ctx2 = ctx.clone(); + ctx2.entryCtx + = new Context.EntryContext(entryYearStr, entryMonthStr, entryDayStr, + entryNumberStr, entryDowStr); + ArrayList childList = getChildList(this.node); ArrayList toProcess = new ArrayList(childList.size()+8); for ( Node child : childList ) { @@ -80,7 +85,7 @@ public final class TodoEntryElement extends TodoDefaultElement { } else if ( child.getNodeType() == Node.ELEMENT_NODE ) { div.appendChild(child); TodoElement it - = TodoElement.getTodoElement((Element)child, this.ctx, this); + = TodoElement.getTodoElement((Element)child, ctx2, this); toProcess.add(it); } } @@ -89,7 +94,7 @@ public final class TodoEntryElement extends TodoDefaultElement { "d:implicit-do-comments"); div.appendChild(token); div.appendChild(ctx.doc.createTextNode("\n")); - // toProcess.add(new TodoComments(token, this.ctx, this)); + toProcess.add(new TodoComments(token, ctx2, this)); } this.ownerDeque.registerAtStart(toProcess); } diff --git a/org/madore/damlengine/TodoWrefAttr.java b/org/madore/damlengine/TodoWrefAttr.java index 5737577..5a15045 100644 --- a/org/madore/damlengine/TodoWrefAttr.java +++ b/org/madore/damlengine/TodoWrefAttr.java @@ -26,7 +26,7 @@ public class TodoWrefAttr extends TodoAttr { String wrefStr = attr.getValue(); Matcher wrefMatcher = Pattern.compile("^\\#d\\.(\\d{4})-(\\d{2})-(\\d{2})\\.(\\d{4})(|\\..*)$").matcher(wrefStr); if ( ! wrefMatcher.matches() ) - throw new IllegalArgumentException("wref attribute ("+wrefStr+") must be of the form #d.YYYY-MM-DD.NNNN[.xxx]"); + throw new IllegalArgumentException("wref attribute must be of the form #d.YYYY-MM-DD.NNNN[.xxx]"); String wrefYearStr = wrefMatcher.group(1); String wrefMonthStr = wrefMatcher.group(2); String wrefDayStr = wrefMatcher.group(3); -- cgit v1.2.3