Improved formatting.

This commit is contained in:
René Jeschke 2012-08-28 17:32:22 +02:00
parent cc1d5c9bef
commit 7eccd8702b
9 changed files with 578 additions and 301 deletions

View File

@ -36,7 +36,7 @@ class Block
public String id = null;
/** Block meta information */
public String meta = "";
/** Constructor. */
public Block()
{

View File

@ -101,7 +101,7 @@ public class Configuration
private Decorator decorator = new DefaultDecorator();
private BlockEmitter codeBlockEmitter = null;
private SpanEmitter specialLinkEmitter = null;
/**
* Constructor.
*
@ -206,7 +206,8 @@ public class Configuration
/**
* Sets the emitter for special link spans ([[ ... ]]).
*
* @param emitter The emitter.
* @param emitter
* The emitter.
* @return This builder.
* @since 0.7
*/
@ -215,7 +216,7 @@ public class Configuration
this.specialLinkEmitter = emitter;
return this;
}
/**
* Builds a configuration instance.
*

View File

@ -25,20 +25,32 @@ public interface Decorator
/**
* Called when a paragraph is opened.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;p>");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;p>");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void openParagraph(final StringBuilder out);
/**
* Called when a paragraph is closed.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;/p>\n");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;/p>\n");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void closeParagraph(final StringBuilder out);
@ -46,236 +58,380 @@ public interface Decorator
* Called when a blockquote is opened.
*
* Default implementation is:
* <pre><code>out.append("&lt;blockquote>");</code></pre>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;blockquote>");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void openBlockquote(final StringBuilder out);
/**
* Called when a blockquote is closed.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;/blockquote>\n");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;/blockquote>\n");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void closeBlockquote(final StringBuilder out);
/**
* Called when a code block is opened.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;pre>&lt;code>");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;pre>&lt;code>");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void openCodeBlock(final StringBuilder out);
/**
* Called when a code block is closed.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;/code>&lt;/pre>\n");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;/code>&lt;/pre>\n");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void closeCodeBlock(final StringBuilder out);
/**
* Called when a code span is opened.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;code>");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;code>");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void openCodeSpan(final StringBuilder out);
/**
* Called when a code span is closed.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;/code>");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;/code>");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void closeCodeSpan(final StringBuilder out);
/**
* Called when a headline is opened.
*
* <p><strong>Note:</strong> Don't close the HTML tag!</p>
* <p>Default implementation is:</p>
* <pre><code> out.append("&lt;h");
* out.append(level);</code></pre>
* <p>
* <strong>Note:</strong> Don't close the HTML tag!
* </p>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code> out.append("&lt;h");
* out.append(level);</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void openHeadline(final StringBuilder out, int level);
/**
* Called when a headline is closed.
*
* <p>Default implementation is:</p>
* <pre><code> out.append("&lt;/h");
* out.append(level);
* out.append(">\n");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code> out.append("&lt;/h");
* out.append(level);
* out.append(">\n");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void closeHeadline(final StringBuilder out, int level);
/**
* Called when a strong span is opened.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;strong>");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;strong>");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void openStrong(final StringBuilder out);
/**
* Called when a strong span is closed.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;/strong>");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;/strong>");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void closeStrong(final StringBuilder out);
/**
* Called when an emphasis span is opened.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;em>");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;em>");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void openEmphasis(final StringBuilder out);
/**
* Called when an emphasis span is closed.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;/em>");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;/em>");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void closeEmphasis(final StringBuilder out);
/**
* Called when a superscript span is opened.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;sup>");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;sup>");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void openSuper(final StringBuilder out);
/**
* Called when a superscript span is closed.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;/sup>");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;/sup>");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void closeSuper(final StringBuilder out);
/**
* Called when an ordered list is opened.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;ol>\n");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;ol>\n");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void openOrderedList(final StringBuilder out);
/**
* Called when an ordered list is closed.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;/ol>\n");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;/ol>\n");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void closeOrderedList(final StringBuilder out);
/**
* Called when an unordered list is opened.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;ul>\n");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;ul>\n");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void openUnorderedList(final StringBuilder out);
/**
* Called when an unordered list is closed.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;/ul>\n");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;/ul>\n");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void closeUnorderedList(final StringBuilder out);
/**
* Called when a list item is opened.
*
* <p><strong>Note:</strong> Don't close the HTML tag!</p>
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;li");</code></pre>
* <p>
* <strong>Note:</strong> Don't close the HTML tag!
* </p>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;li");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void openListItem(final StringBuilder out);
/**
* Called when a list item is closed.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;/li>\n");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;/li>\n");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void closeListItem(final StringBuilder out);
/**
* Called when a horizontal ruler is encountered.
*
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;hr />\n");</code></pre>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;hr />\n");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void horizontalRuler(final StringBuilder out);
/**
* Called when a link is opened.
*
* <p><strong>Note:</strong> Don't close the HTML tag!</p>
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;a");</code></pre>
* <p>
* <strong>Note:</strong> Don't close the HTML tag!
* </p>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;a");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void openLink(final StringBuilder out);
/**
* Called when an image is opened.
*
* <p><strong>Note:</strong> Don't close the HTML tag!</p>
* <p>Default implementation is:</p>
* <pre><code>out.append("&lt;img");</code></pre>
* <p>
* <strong>Note:</strong> Don't close the HTML tag!
* </p>
* <p>
* Default implementation is:
* </p>
*
* @param out The StringBuilder to write to.
* <pre>
* <code>out.append("&lt;img");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void openImage(final StringBuilder out);
}

View File

@ -31,7 +31,7 @@ class Emitter
private final Configuration config;
/** Extension flag. */
public boolean useExtensions = false;
/** Constructor. */
public Emitter(final Configuration config)
{
@ -42,8 +42,10 @@ class Emitter
/**
* Adds a LinkRef to this set of LinkRefs.
*
* @param key The key/id.
* @param linkRef The LinkRef.
* @param key
* The key/id.
* @param linkRef
* The LinkRef.
*/
public void addLinkRef(final String key, final LinkRef linkRef)
{
@ -53,8 +55,10 @@ class Emitter
/**
* Transforms the given block recursively into HTML.
*
* @param out The StringBuilder to write to.
* @param root The Block to process.
* @param out
* The StringBuilder to write to.
* @param root
* The Block to process.
*/
public void emit(final StringBuilder out, final Block root)
{
@ -156,8 +160,10 @@ class Emitter
/**
* Transforms lines into HTML.
*
* @param out The StringBuilder to write to.
* @param block The Block to process.
* @param out
* The StringBuilder to write to.
* @param block
* The Block to process.
*/
private void emitLines(final StringBuilder out, final Block block)
{
@ -181,9 +187,12 @@ class Emitter
/**
* Finds the position of the given Token in the given String.
*
* @param in The String to search on.
* @param start The starting character position.
* @param token The token to find.
* @param in
* The String to search on.
* @param start
* The starting character position.
* @param token
* The token to find.
* @return The position of the token or -1 if none could be found.
*/
private int findToken(final String in, int start, MarkToken token)
@ -201,10 +210,14 @@ class Emitter
/**
* Checks if there is a valid markdown link definition.
*
* @param out The StringBuilder containing the generated output.
* @param in Input String.
* @param start Starting position.
* @param token Either LINK or IMAGE.
* @param out
* The StringBuilder containing the generated output.
* @param in
* Input String.
* @param start
* Starting position.
* @param token
* Either LINK or IMAGE.
* @return The new position or -1 if there is no valid markdown link.
*/
private int checkLink(final StringBuilder out, final String in, int start, MarkToken token)
@ -212,7 +225,7 @@ class Emitter
boolean isAbbrev = false;
int pos = start + (token == MarkToken.LINK ? 1 : 2);
final StringBuilder temp = new StringBuilder();
temp.setLength(0);
pos = Utils.readMdLinkId(temp, in, pos);
if(pos < start)
@ -355,12 +368,15 @@ class Emitter
}
/**
* Check if there is a valid HTML tag here.
* This method also transforms auto links and mailto auto links.
* Check if there is a valid HTML tag here. This method also transforms auto
* links and mailto auto links.
*
* @param out The StringBuilder to write to.
* @param in Input String.
* @param start Starting position.
* @param out
* The StringBuilder to write to.
* @param in
* Input String.
* @param start
* Starting position.
* @return The new position or -1 if nothing valid has been found.
*/
private int checkHtml(final StringBuilder out, final String in, int start)
@ -386,7 +402,7 @@ class Emitter
return pos;
}
}
// Check for mailto auto link
temp.setLength(0);
pos = Utils.readUntil(temp, in, start + 1, '@', ' ', '>', '\n');
@ -406,23 +422,26 @@ class Emitter
return pos;
}
}
// Check for inline html
if(start + 2 < in.length())
{
temp.setLength(0);
return Utils.readXML(out, in, start, this.config.safeMode);
}
}
return -1;
}
/**
* Check if this is a valid XML/HTML entity.
*
* @param out The StringBuilder to write to.
* @param in Input String.
* @param start Starting position
* @param out
* The StringBuilder to write to.
* @param in
* Input String.
* @param start
* Starting position
* @return The new position or -1 if this entity in invalid.
*/
private static int checkEntity(final StringBuilder out, final String in, int start)
@ -465,18 +484,24 @@ class Emitter
out.append(';');
return HTML.isEntity(out.toString()) ? pos : -1;
}
return pos;
}
/**
* Recursively scans through the given line, taking care of any markdown stuff.
* Recursively scans through the given line, taking care of any markdown
* stuff.
*
* @param out The StringBuilder to write to.
* @param in Input String.
* @param start Start position.
* @param token The matching Token (for e.g. '*')
* @return The position of the matching Token or -1 if token was NONE or no Token could be found.
* @param out
* The StringBuilder to write to.
* @param in
* Input String.
* @param start
* Start position.
* @param token
* The matching Token (for e.g. '*')
* @return The position of the matching Token or -1 if token was NONE or no
* Token could be found.
*/
private int recursiveEmitLine(final StringBuilder out, final String in, int start, MarkToken token)
{
@ -485,7 +510,9 @@ class Emitter
while(pos < in.length())
{
final MarkToken mt = this.getToken(in, pos);
if(token != MarkToken.NONE && (mt == token || token == MarkToken.EM_STAR && mt == MarkToken.STRONG_STAR || token == MarkToken.EM_UNDERSCORE && mt == MarkToken.STRONG_UNDERSCORE))
if(token != MarkToken.NONE
&& (mt == token || token == MarkToken.EM_STAR && mt == MarkToken.STRONG_STAR || token == MarkToken.EM_UNDERSCORE
&& mt == MarkToken.STRONG_UNDERSCORE))
return pos;
switch(mt)
@ -666,14 +693,16 @@ class Emitter
/**
* Check if there is any markdown Token.
*
* @param in Input String.
* @param pos Starting position.
* @param in
* Input String.
* @param pos
* Starting position.
* @return The Token.
*/
private MarkToken getToken(final String in, final int pos)
{
final char c0 = pos > 0 ? in.charAt(pos - 1) : ' ';
final char c = in.charAt(pos);
final char c = in.charAt(pos);
final char c1 = pos + 1 < in.length() ? in.charAt(pos + 1) : ' ';
final char c2 = pos + 2 < in.length() ? in.charAt(pos + 2) : ' ';
final char c3 = pos + 3 < in.length() ? in.charAt(pos + 3) : ' ';
@ -785,8 +814,10 @@ class Emitter
/**
* Writes a set of markdown lines into the StringBuilder.
*
* @param out The StringBuilder to write to.
* @param lines The lines to write.
* @param out
* The StringBuilder to write to.
* @param lines
* The lines to write.
*/
private void emitMarkedLines(final StringBuilder out, final Line lines)
{
@ -811,8 +842,10 @@ class Emitter
/**
* Writes a set of raw lines into the StringBuilder.
*
* @param out The StringBuilder to write to.
* @param lines The lines to write.
* @param out
* The StringBuilder to write to.
* @param lines
* The lines to write.
*/
private void emitRawLines(final StringBuilder out, final Line lines)
{
@ -869,9 +902,12 @@ class Emitter
/**
* Writes a code block into the StringBuilder.
*
* @param out The StringBuilder to write to.
* @param lines The lines to write.
* @param meta Meta information.
* @param out
* The StringBuilder to write to.
* @param lines
* The lines to write.
* @param meta
* Meta information.
*/
private void emitCodeLines(final StringBuilder out, final Line lines, final String meta, final boolean removeIndent)
{

View File

@ -32,8 +32,10 @@ class LinkRef
/**
* Constructor.
*
* @param link The link.
* @param title The title (may be <code>null</code>).
* @param link
* The link.
* @param title
* The title (may be <code>null</code>).
*/
public LinkRef(final String link, final String title, final boolean isAbbrev)
{

View File

@ -269,7 +269,8 @@ public class Processor
* if an IO error occurs
* @see Configuration#DEFAULT
*/
public final static String process(final File file, final Decorator decorator, final boolean safeMode) throws IOException
public final static String process(final File file, final Decorator decorator, final boolean safeMode)
throws IOException
{
return process(file, Configuration.builder().setDecorator(decorator).setSafeMode(safeMode).build());
}
@ -305,7 +306,8 @@ public class Processor
* if an IO error occurs
* @see Configuration#DEFAULT
*/
public final static String process(final File file, final String encoding, final boolean safeMode) throws IOException
public final static String process(final File file, final String encoding, final boolean safeMode)
throws IOException
{
return process(file, Configuration.builder().setEncoding(encoding).setSafeMode(safeMode).build());
}
@ -324,7 +326,8 @@ public class Processor
* if an IO error occurs
* @see Configuration#DEFAULT
*/
public final static String process(final File file, final String encoding, final Decorator decorator) throws IOException
public final static String process(final File file, final String encoding, final Decorator decorator)
throws IOException
{
return process(file, Configuration.builder().setEncoding(encoding).setDecorator(decorator).build());
}
@ -869,7 +872,8 @@ public class Processor
{
if(line.getLineType(this.useExtensions) == LineType.FENCED_CODE)
break;
// TODO ... is this really necessary? Maybe add a special flag?
// TODO ... is this really necessary? Maybe add a special
// flag?
line = line.next;
}
if(line != null)

View File

@ -24,30 +24,41 @@ import java.io.InputStreamReader;
/**
* Simple class for processing markdown files on the command line.
*
* <p>Usage:</p>
* <pre><code>java -cp txtmark.jar txtmark.Run filename [header_footer_file]
* </code></pre>
* <p>
* Usage:
* </p>
*
* <p>The <code>header_footer_file</code> is an optional UTF-8 encoded file containing
* a header and a footer to output around the generated HTML code.</p>
* <pre>
* <code>java -cp txtmark.jar txtmark.Run filename [header_footer_file]
* </code>
* </pre>
*
* <p>Example:</p>
* <p>
* The <code>header_footer_file</code> is an optional UTF-8 encoded file
* containing a header and a footer to output around the generated HTML code.
* </p>
*
* <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?>
*&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
* "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
*&lt;html xmlns="http://www.w3.org/1999/xhtml">
*&lt;head>
*&lt;title>markdown&lt;/title>
*&lt;link type="text/css" href="style.css" rel="stylesheet"/>
*&lt;meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
*&lt;/head>
*&lt;body>
*&lt;!-- the following line separates header from footer -->
*&lt;!-- ### -->
*&lt;/body>
*&lt;/html>
*</code></pre>
* <p>
* Example:
* </p>
*
* <pre>
* <code>&lt;?xml version="1.0" encoding="UTF-8"?>
* &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
* "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
* &lt;html xmlns="http://www.w3.org/1999/xhtml">
* &lt;head>
* &lt;title>markdown&lt;/title>
* &lt;link type="text/css" href="style.css" rel="stylesheet"/>
* &lt;meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
* &lt;/head>
* &lt;body>
* &lt;!-- the following line separates header from footer -->
* &lt;!-- ### -->
* &lt;/body>
* &lt;/html>
* </code>
* </pre>
*
* @author René Jeschke <rene_jeschke@yahoo.de>
*/
@ -56,8 +67,10 @@ public class Run
/**
* Static main.
*
* @param args Program arguments.
* @throws IOException If an IO error occurred.
* @param args
* Program arguments.
* @throws IOException
* If an IO error occurred.
*/
public static void main(String[] args) throws IOException
{

View File

@ -16,18 +16,20 @@
package com.github.rjeschke.txtmark;
/**
* An interface for emitting span elements. Currently only
* used for special links.
* An interface for emitting span elements. Currently only used for special
* links.
*
* @author René Jeschke (rene_jeschke@yahoo.de)
*/
public interface SpanEmitter
{
/**
* Emits a span element.
* Emits a span element.
*
* @param out The StringBuilder to append to.
* @param content The span's content.
* @param out
* The StringBuilder to append to.
* @param content
* The span's content.
*/
public void emitSpan(StringBuilder out, String content);
}

View File

@ -24,7 +24,7 @@ class Utils
{
/** Random number generator value. */
private static int RND = (int)System.nanoTime();
/**
* LCG random number generator.
*
@ -34,12 +34,14 @@ class Utils
{
return (RND = RND * 1664525 + 1013904223) >>> 22;
}
/**
* Skips spaces in the given String.
*
* @param in Input String.
* @param start Starting position.
* @param in
* Input String.
* @param start
* Starting position.
* @return The new position or -1 if EOL has been reached.
*/
public final static int skipSpaces(final String in, final int start)
@ -53,9 +55,12 @@ class Utils
/**
* Processed the given escape sequence.
*
* @param out The StringBuilder to write to.
* @param ch The character.
* @param pos Current parsing position.
* @param out
* The StringBuilder to write to.
* @param ch
* The character.
* @param pos
* Current parsing position.
* @return The new position.
*/
public final static int escape(final StringBuilder out, final char ch, final int pos)
@ -89,14 +94,18 @@ class Utils
return pos;
}
}
/**
* Reads characters until any 'end' character is encountered.
*
* @param out The StringBuilder to write to.
* @param in The Input String.
* @param start Starting position.
* @param end End characters.
* @param out
* The StringBuilder to write to.
* @param in
* The Input String.
* @param start
* Starting position.
* @param end
* End characters.
* @return The new position or -1 if no 'end' char was found.
*/
public final static int readUntil(final StringBuilder out, final String in, final int start, final char... end)
@ -133,10 +142,14 @@ class Utils
/**
* Reads characters until the 'end' character is encountered.
*
* @param out The StringBuilder to write to.
* @param in The Input String.
* @param start Starting position.
* @param end End characters.
* @param out
* The StringBuilder to write to.
* @param in
* The Input String.
* @param start
* Starting position.
* @param end
* End characters.
* @return The new position or -1 if no 'end' char was found.
*/
public final static int readUntil(final StringBuilder out, final String in, final int start, final char end)
@ -164,9 +177,12 @@ class Utils
/**
* Reads a markdown link.
*
* @param out The StringBuilder to write to.
* @param in Input String.
* @param start Starting position.
* @param out
* The StringBuilder to write to.
* @param in
* Input String.
* @param start
* Starting position.
* @return The new position or -1 if this is no valid markdown link.
*/
public final static int readMdLink(final StringBuilder out, final String in, final int start)
@ -211,9 +227,12 @@ class Utils
/**
* Reads a markdown link ID.
*
* @param out The StringBuilder to write to.
* @param in Input String.
* @param start Starting position.
* @param out
* The StringBuilder to write to.
* @param in
* Input String.
* @param start
* Starting position.
* @return The new position or -1 if this is no valid markdown link ID.
*/
public final static int readMdLinkId(final StringBuilder out, final String in, final int start)
@ -253,12 +272,17 @@ class Utils
}
/**
* Reads characters until any 'end' character is encountered, ignoring escape sequences.
* Reads characters until any 'end' character is encountered, ignoring
* escape sequences.
*
* @param out The StringBuilder to write to.
* @param in The Input String.
* @param start Starting position.
* @param end End characters.
* @param out
* The StringBuilder to write to.
* @param in
* The Input String.
* @param start
* Starting position.
* @param end
* End characters.
* @return The new position or -1 if no 'end' char was found.
*/
public final static int readRawUntil(final StringBuilder out, final String in, final int start, final char... end)
@ -286,12 +310,17 @@ class Utils
}
/**
* Reads characters until the end character is encountered, ignoring escape sequences.
* Reads characters until the end character is encountered, ignoring escape
* sequences.
*
* @param out The StringBuilder to write to.
* @param in The Input String.
* @param start Starting position.
* @param end End characters.
* @param out
* The StringBuilder to write to.
* @param in
* The Input String.
* @param start
* Starting position.
* @param end
* End characters.
* @return The new position or -1 if no 'end' char was found.
*/
public final static int readRawUntil(final StringBuilder out, final String in, final int start, final char end)
@ -312,10 +341,14 @@ class Utils
/**
* Appends the given string encoding special HTML characters.
*
* @param out The StringBuilder to write to.
* @param in Input String.
* @param start Input String starting position.
* @param end Input String end position.
* @param out
* The StringBuilder to write to.
* @param in
* Input String.
* @param start
* Input String starting position.
* @param end
* Input String end position.
*/
public final static void appendCode(final StringBuilder out, final String in, final int start, final int end)
{
@ -341,12 +374,17 @@ class Utils
}
/**
* Appends the given string encoding special HTML characters (used in HTML attribute values).
* Appends the given string encoding special HTML characters (used in HTML
* attribute values).
*
* @param out The StringBuilder to write to.
* @param in Input String.
* @param start Input String starting position.
* @param end Input String end position.
* @param out
* The StringBuilder to write to.
* @param in
* Input String.
* @param start
* Input String starting position.
* @param end
* Input String end position.
*/
public final static void appendValue(final StringBuilder out, final String in, final int start, final int end)
{
@ -380,8 +418,10 @@ class Utils
/**
* Append the given char as a decimal HTML entity.
*
* @param out The StringBuilder to write to.
* @param value The character.
* @param out
* The StringBuilder to write to.
* @param value
* The character.
*/
public final static void appendDecEntity(final StringBuilder out, final char value)
{
@ -389,12 +429,14 @@ class Utils
out.append((int)value);
out.append(';');
}
/**
* Append the given char as a hexadecimal HTML entity.
*
* @param out The StringBuilder to write to.
* @param value The character.
* @param out
* The StringBuilder to write to.
* @param value
* The character.
*/
public final static void appendHexEntity(final StringBuilder out, final char value)
{
@ -406,10 +448,14 @@ class Utils
/**
* Appends the given mailto link using obfuscation.
*
* @param out The StringBuilder to write to.
* @param in Input String.
* @param start Input String starting position.
* @param end Input String end position.
* @param out
* The StringBuilder to write to.
* @param in
* Input String.
* @param start
* Input String starting position.
* @param end
* Input String end position.
*/
public final static void appendMailto(final StringBuilder out, final String in, final int start, final int end)
{
@ -441,12 +487,14 @@ class Utils
}
}
}
/**
* Extracts the tag from an XML element.
*
* @param out The StringBuilder to write to.
* @param in Input StringBuilder.
* @param out
* The StringBuilder to write to.
* @param in
* Input StringBuilder.
*/
public final static void getXMLTag(final StringBuilder out, final StringBuilder in)
{
@ -458,12 +506,14 @@ class Utils
out.append(in.charAt(pos++));
}
}
/**
* Extracts the tag from an XML element.
*
* @param out The StringBuilder to write to.
* @param in Input String.
* @param out
* The StringBuilder to write to.
* @param in
* Input String.
*/
public final static void getXMLTag(final StringBuilder out, final String in)
{
@ -479,80 +529,92 @@ class Utils
/**
* Reads an XML element.
*
* @param out The StringBuilder to write to.
* @param in Input String.
* @param start Starting position.
* @param safeMode Whether to escape unsafe HTML tags or not
* @param out
* The StringBuilder to write to.
* @param in
* Input String.
* @param start
* Starting position.
* @param safeMode
* Whether to escape unsafe HTML tags or not
* @return The new position or -1 if this is no valid XML element.
*/
public final static int readXML(final StringBuilder out, final String in, final int start, final boolean safeMode)
{
int pos;
final boolean isCloseTag;
try {
try
{
if(in.charAt(start + 1) == '/')
{
isCloseTag = true;
pos = start + 2;
}
else if(in.charAt(start + 1) == '!')
{
out.append("<!");
return start + 1;
}
else
{
isCloseTag = false;
pos = start + 1;
}
if(safeMode)
{
final StringBuilder temp = new StringBuilder();
pos = readRawUntil(temp, in, pos, ' ', '/', '>');
if(pos == -1) return -1;
final String tag = temp.toString().trim().toLowerCase();
if(HTML.isUnsafeHtmlElement(tag))
{
out.append("&lt;");
if(isCloseTag)
out.append('/');
out.append(temp);
}
}
else
{
out.append('<');
if(isCloseTag)
out.append('/');
pos = readRawUntil(out, in, pos, ' ', '/', '>');
}
if(pos == -1) return -1;
pos = readRawUntil(out, in, pos, '/', '>');
if(in.charAt(pos) == '/')
{
out.append(" /");
pos = readRawUntil(out, in, pos + 1, '>');
if(pos == -1)
return -1;
}
if(in.charAt(pos) == '>')
{
out.append('>');
return pos;
}
} catch (StringIndexOutOfBoundsException e) {
{
isCloseTag = true;
pos = start + 2;
}
else if(in.charAt(start + 1) == '!')
{
out.append("<!");
return start + 1;
}
else
{
isCloseTag = false;
pos = start + 1;
}
if(safeMode)
{
final StringBuilder temp = new StringBuilder();
pos = readRawUntil(temp, in, pos, ' ', '/', '>');
if(pos == -1)
return -1;
final String tag = temp.toString().trim().toLowerCase();
if(HTML.isUnsafeHtmlElement(tag))
{
out.append("&lt;");
if(isCloseTag)
out.append('/');
out.append(temp);
}
}
else
{
out.append('<');
if(isCloseTag)
out.append('/');
pos = readRawUntil(out, in, pos, ' ', '/', '>');
}
if(pos == -1)
return -1;
pos = readRawUntil(out, in, pos, '/', '>');
if(in.charAt(pos) == '/')
{
out.append(" /");
pos = readRawUntil(out, in, pos + 1, '>');
if(pos == -1)
return -1;
}
if(in.charAt(pos) == '>')
{
out.append('>');
return pos;
}
}
catch (StringIndexOutOfBoundsException e)
{
return -1;
}
return -1;
}
/**
* Appends the given string to the given StringBuilder, replacing
* '&amp;', '&lt;' and '&gt;' by their respective HTML entities.
* Appends the given string to the given StringBuilder, replacing '&amp;',
* '&lt;' and '&gt;' by their respective HTML entities.
*
* @param out The StringBuilder to append to.
* @param value The string to append.
* @param offset The character offset into value from where to start
* @param out
* The StringBuilder to append to.
* @param value
* The string to append.
* @param offset
* The character offset into value from where to start
*/
public final static void codeEncode(StringBuilder out, String value, int offset)
{
@ -575,11 +637,12 @@ class Utils
}
}
}
/**
* Removes trailing <code>`</code> and trims spaces.
*
* @param fenceLine Fenced code block starting line
* @param fenceLine
* Fenced code block starting line
* @return Rest of the line after trimming and backtick removal
* @since 0.7
*/