Skip to content

Commit

Permalink
quick and dirty fix for special chars encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
enr committed May 19, 2010
1 parent dace1f2 commit a9470bb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
25 changes: 18 additions & 7 deletions core/src/main/java/com/petebevin/markdown/Entities.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@
public class Entities
{
/**
* A map of HTML 4.01 entities, taken from W3C website.
* A map of HTML 4.01 entities, taken from W3C website, without
* chars used in tags such as greater-than, less-than, ampersand and quotation mark.
*
* @see http://www.w3.org/TR/html401/sgml/entities.html
*/
public static final Map<Character, String> HTML_401 = new HashMap<Character, String>() {
public static final Map<Character, String> HTML_401_NO_TAG = new HashMap<Character, String>() {
{
//put(Character.valueOf((char) 34), "&#34;"); // quotation mark
//put(Character.valueOf((char) 38), "&#38;"); // ampersand
//put(Character.valueOf((char) 60), "&#60;"); // less-than
//put(Character.valueOf((char) 62), "&#62;"); // greater-than
put(Character.valueOf((char) 160), "&#160;");
put(Character.valueOf((char) 161), "&#161;");
put(Character.valueOf((char) 162), "&#162;");
Expand Down Expand Up @@ -266,7 +263,21 @@ public class Entities
put(Character.valueOf((char) 8364), "&#8364;");
}
};


/**
* Full map of HTML 4.01 entities, taken from W3C website.
*
* @see http://www.w3.org/TR/html401/sgml/entities.html
*/
public static final Map<Character, String> HTML_401 = new HashMap<Character, String>(HTML_401_NO_TAG) {
{
put(Character.valueOf((char) 34), "&#34;"); // quotation mark
put(Character.valueOf((char) 38), "&#38;"); // ampersand
put(Character.valueOf((char) 60), "&#60;"); // less-than
put(Character.valueOf((char) 62), "&#62;"); // greater-than
}
};

public static String encode(String text, Map<Character, String> entities)
{
o(text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void testHtmlEntitiesCustomSetting() {
public void testHtmlEntities() {
String md = "XXXI paid 2 £ this nòrmal paragraph & <àccènts>!";
String expected = "<p>XXXI paid 2 &#163; this n&#242;rmal paragraph &amp; &lt;&#224;cc&#232;nts>!</p>\n";
m.setHtmlEntities(Entities.HTML_401);
m.setHtmlEntities(Entities.HTML_401_NO_TAG);
String html = m.markdown(md);
assertEquals(expected, html);
}
Expand All @@ -86,15 +86,15 @@ public void testHtmlEntities() {
public void testCodeBlock() {
String md = "fìrst linè\n\n\tthìs ìs còde\n";
String expected = "<p>f&#236;rst lin&#232;</p>\n\n<pre><code>th&#236;s &#236;s c&#242;de\n</code></pre>\n";
m.setHtmlEntities(Entities.HTML_401);
m.setHtmlEntities(Entities.HTML_401_NO_TAG);
String html = m.markdown(md);
assertEquals(expected, html);
}
@Test
public void testAdvancedSyntaxAndStrictHtmlEncoding() {
String md = "XXXAnother <http://example.tld> link";
String expected = "<p>XXXAnother <a href=\"http://example.tld\">http://example.tld</a> link</p>\n";
m.setHtmlEntities(Entities.HTML_401);
m.setHtmlEntities(Entities.HTML_401_NO_TAG);
String html = m.markdown(md);
assertEquals(expected, html);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private static String chomp(String s) {
@Test
public void runTest() {
MarkdownProcessor markup = new MarkdownProcessor();
markup.setHtmlEntities(Entities.HTML_401);
markup.setHtmlEntities(Entities.HTML_401_NO_TAG);
assertEquals(pair.toString(), pair.getResult().trim(), markup.markdown(pair.getTest()).trim());
}
}

0 comments on commit a9470bb

Please sign in to comment.