r507 - in trunk/jrst/src: main/java/org/nuiton/jrst test/java/org/nuiton/jrst test/resources
Author: vbriand Date: 2010-11-19 11:17:13 +0100 (Fri, 19 Nov 2010) New Revision: 507 Url: http://nuiton.org/repositories/revision/jrst/507 Log: Special characters in hyperlinks targets are now managed correctly Added: trunk/jrst/src/test/resources/testHyperlinkTarget.rst trunk/jrst/src/test/resources/testHyperlinkTarget.xml Modified: trunk/jrst/src/main/java/org/nuiton/jrst/JRSTLexer.java trunk/jrst/src/main/java/org/nuiton/jrst/JRSTReader.java trunk/jrst/src/test/java/org/nuiton/jrst/JRSTReaderTest.java Modified: trunk/jrst/src/main/java/org/nuiton/jrst/JRSTLexer.java =================================================================== --- trunk/jrst/src/main/java/org/nuiton/jrst/JRSTLexer.java 2010-11-12 17:13:57 UTC (rev 506) +++ trunk/jrst/src/main/java/org/nuiton/jrst/JRSTLexer.java 2010-11-19 10:17:13 UTC (rev 507) @@ -32,9 +32,12 @@ import java.io.IOException; import java.io.Reader; +import java.net.URLEncoder; +import java.text.Normalizer; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; +import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -1994,7 +1997,7 @@ endPeek(); return result; } - + public Element peekTarget() throws IOException { beginPeek(); @@ -2006,8 +2009,8 @@ Matcher matcher = Pattern.compile("\\.\\.\\s_").matcher(line); if (matcher.find()) { int i = line.indexOf(':'); - result.addAttribute("id", line.substring(matcher.end(), i) - .replaceAll("\\W", "-").toLowerCase()); + result.addAttribute("id", URLEncoder.encode(line.substring(matcher.end(), i) + .toLowerCase().replaceAll(" ", "-"), "UTF-8")); result.addAttribute("level", "" + level(line)); } } @@ -2039,9 +2042,8 @@ "" + level(line)); result.getLast().addAttribute( "id", - line.substring(matcher.end(), i) - .replaceAll("\\W", "-") - .toLowerCase()); + URLEncoder.encode(line.substring(matcher.end(), i) + .replaceAll(" ", "-").toLowerCase(), "UTF-8")); result.getLast().addAttribute( "name", line.substring(matcher.end(), i) Modified: trunk/jrst/src/main/java/org/nuiton/jrst/JRSTReader.java =================================================================== --- trunk/jrst/src/main/java/org/nuiton/jrst/JRSTReader.java 2010-11-12 17:13:57 UTC (rev 506) +++ trunk/jrst/src/main/java/org/nuiton/jrst/JRSTReader.java 2010-11-19 10:17:13 UTC (rev 507) @@ -121,7 +121,10 @@ import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; +import java.io.UnsupportedEncodingException; import java.net.URL; +import java.net.URLEncoder; +import java.text.Normalizer; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -144,6 +147,7 @@ import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; +import org.dom4j.IllegalAddException; import org.dom4j.Node; import org.dom4j.VisitorSupport; @@ -479,7 +483,11 @@ if (log.isWarnEnabled()) { log.warn("Can't inline text for " + e, eee); } - } + } catch (UnsupportedEncodingException ee) { + if (log.isWarnEnabled()) { + log.warn("Unsupported encoding " + e, ee); + } + } } } }); @@ -894,7 +902,7 @@ while (!lexer.eof() && itemNotEquals(TITLE, item) && isUpperLevel(item, parent)) { if (itemEquals(JRSTLexer.BLANK_LINE, item)) { - // go t o the next element + // go to the next element lexer.remove(); } else if (itemEquals("remove", item)) { lexer.remove(); @@ -973,7 +981,12 @@ } else if (itemEquals(TARGET, item)) { lexer.remove(); Element list = composeTarget(item); - parent.add(list); + if (list != null) { + try { + parent.add(list); + } catch (IllegalAddException e) {} + } else + System.err.println("Unknown target name : \"" + item.attributeValue("id") + "\""); } else if (itemEquals("targetAnonymous", item)) { lexer.remove(); Element list = composeTargetAnonymous(item); @@ -2047,8 +2060,10 @@ * @param Element * e * @throws DocumentException + * @throws UnsupportedEncodingException */ - private void inline(Element e) throws DocumentException { + + private void inline(Element e) throws DocumentException, UnsupportedEncodingException { String text = e.getText(); text = StringEscapeUtils.escapeXml(text); @@ -2251,14 +2266,15 @@ String ref = text.substring(matcher.start(), matcher.end() - 1); ref = ref.replaceAll("('|_)", ""); - ref = ref.replaceAll("[\\W&&[^-]]", " ").trim(); + ref = StringEscapeUtils.unescapeXml(ref); + ref = ref.replaceAll("`", ""); Element hyper = DocumentHelper.createElement("reference"); hyper.addAttribute("name", ref); boolean trouve = false; for (int i = 0; i < eTarget.size() && !trouve; i++) { Element el = eTarget.get(i); - String refTmp = ref.replaceAll("\\s", "-").toLowerCase(); - if (el.attributeValue("id").equalsIgnoreCase(refTmp)) { + String refTmp = URLEncoder.encode(ref.replaceAll("\\s", "-").toLowerCase(), "UTF-8"); + if (el.attributeValue("id").equalsIgnoreCase((refTmp))) { hyper.addAttribute("refuri", el.attributeValue("refuri")); trouve = true; } Modified: trunk/jrst/src/test/java/org/nuiton/jrst/JRSTReaderTest.java =================================================================== --- trunk/jrst/src/test/java/org/nuiton/jrst/JRSTReaderTest.java 2010-11-12 17:13:57 UTC (rev 506) +++ trunk/jrst/src/test/java/org/nuiton/jrst/JRSTReaderTest.java 2010-11-19 10:17:13 UTC (rev 507) @@ -29,6 +29,8 @@ import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; +import java.io.BufferedReader; +import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; @@ -104,4 +106,23 @@ "</document>"; Assert.assertEquals(XMLexpected, doc.asXML()); } + + @Test + public void testHyperlinkTarget() throws Exception { + URL url = JRSTReaderTest.class.getResource("/testHyperlinkTarget.rst"); + Reader in = new InputStreamReader(url.openStream()); + JRSTReader jrst = new JRSTReader(); + Document doc = jrst.read(in); + URL xmlUrl = JRSTReaderTest.class.getResource("/testHyperlinkTarget.xml"); + Reader xmlIn = new InputStreamReader(xmlUrl.openStream()); + BufferedReader reader = new BufferedReader(xmlIn); + String line = null; + StringBuilder stringBuilder = new StringBuilder(); + String ls = System.getProperty("line.separator"); + while ((line = reader.readLine()) != null) { + stringBuilder.append(line); + stringBuilder.append(ls); + } + Assert.assertEquals(stringBuilder.toString(), doc.asXML() + "\n"); + } } Added: trunk/jrst/src/test/resources/testHyperlinkTarget.rst =================================================================== --- trunk/jrst/src/test/resources/testHyperlinkTarget.rst (rev 0) +++ trunk/jrst/src/test/resources/testHyperlinkTarget.rst 2010-11-19 10:17:13 UTC (rev 507) @@ -0,0 +1,19 @@ +(External) hyperlinks, like Python_. + +.. _Python: http://www.python.org/ + +(External) hyperlinks, like Pythoné_. + +.. _Pythoné: http://www.python.org/ + +- `Module de depouillement (votecounting)`_ + +.. _Module de depouillement (votecounting): http://www.votecounting.com + +- `Module de depouillement (votecounting)!@#$%&*`_ + +.. _Module de depouillement (votecounting)!@#$%&*: http://www.votecounting.com + +- `Module de dépouillement (votecounting)`_ + +.. _Module de dépouillement (votecounting): votecounting.html Added: trunk/jrst/src/test/resources/testHyperlinkTarget.xml =================================================================== --- trunk/jrst/src/test/resources/testHyperlinkTarget.xml (rev 0) +++ trunk/jrst/src/test/resources/testHyperlinkTarget.xml 2010-11-19 10:17:13 UTC (rev 507) @@ -0,0 +1,2 @@ +<?xml version="1.0" encoding="UTF-8"?> +<document><paragraph>(External) hyperlinks, like <reference name="Python" refuri="http://www.python.org/">Python</reference></paragraph><target id="python" name="python" refuri="http://www.python.org/"/><paragraph>(External) hyperlinks, like <reference name="Pythoné" refuri="http://www.python.org/">Pythoné</reference></paragraph><target id="python%C3%A9" name="pythoné" refuri="http://www.python.org/"/><bullet_list bullet="-"><list_item><paragraph><reference name="Module de depouillement (votecounting)" refuri="http://www.votecounting.com">Module de depouillement (votecounting)</reference></paragraph></list_item></bullet_list><target id="module-de-depouillement-%28votecounting%29" name="module de depouillement (votecounting)" refuri="http://www.votecounting.com"/><bullet_list bullet="-"><list_item><paragraph><reference name="Module de depouillement (votecounting)!@#$%&*" refuri="http://www.votecounting.com">Module de depouillement (votecounting)!@#$%&*</reference></paragraph></list_item></bullet_list><target id="module-de-depouillement-%28votecounting%29%21%40%23%24%25%26*" name="module de depouillement (votecounting)!@#$%&*" refuri="http://www.votecounting.com"/><bullet_list bullet="-"><list_item><paragraph><reference name="Module de dépouillement (votecounting)" refuri="votecounting.html">Module de dépouillement (votecounting)</reference></paragraph></list_item></bullet_list><target id="module-de-d%C3%A9pouillement-%28votecounting%29" name="module de dépouillement (votecounting)" refuri="votecounting.html"/></document> \ No newline at end of file Property changes on: trunk/jrst/src/test/resources/testHyperlinkTarget.xml ___________________________________________________________________ Added: svn:mime-type + text/plain
participants (1)
-
vbriand@users.nuiton.org