Package org.commonmark.renderer.markdown
Class MarkdownWriter
- java.lang.Object
-
- org.commonmark.renderer.markdown.MarkdownWriter
-
public class MarkdownWriter extends java.lang.Object
Writer for Markdown (CommonMark) text.
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
atLineStart
private int
blockSeparator
private java.lang.Appendable
buffer
private char
lastChar
private java.util.LinkedList<java.lang.String>
prefixes
private java.util.LinkedList<CharMatcher>
rawEscapes
private java.util.LinkedList<java.lang.Boolean>
tight
-
Constructor Summary
Constructors Constructor Description MarkdownWriter(java.lang.Appendable out)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
append(char c, CharMatcher escape)
void
block()
Enqueue a block separator to be written before the next text is written.private void
flushBlockSeparator()
If a block separator has been enqueued withblock()
but not yet written, write it now.char
getLastChar()
boolean
isAtLineStart()
private boolean
isTight()
void
line()
Write a newline (line terminator).private boolean
needsEscaping(char c, CharMatcher escape)
void
popPrefix()
Remove the last prefix from the top of the stack.void
popRawEscape()
Remove the last raw escape from the top of the stack.void
popTight()
Remove the last "tight" setting from the top of the stack.void
pushPrefix(java.lang.String prefix)
Push a prefix onto the top of the stack.void
pushRawEscape(CharMatcher rawEscape)
Escape the characters matching the supplied matcher, in all text (text and raw).void
pushTight(boolean tight)
Change whether blocks are tight or loose.void
raw(char c)
Write the supplied character (raw/unescaped except ifpushRawEscape(org.commonmark.text.CharMatcher)
was used).void
raw(java.lang.String s)
Write the supplied string (raw/unescaped except ifpushRawEscape(org.commonmark.text.CharMatcher)
was used).private boolean
rawNeedsEscaping(char c)
void
text(java.lang.String s, CharMatcher escape)
Write the supplied string with escaping.private void
write(char c)
private void
write(java.lang.String s, CharMatcher escape)
void
writePrefix(java.lang.String prefix)
Write a prefix.private void
writePrefixes()
-
-
-
Field Detail
-
buffer
private final java.lang.Appendable buffer
-
blockSeparator
private int blockSeparator
-
lastChar
private char lastChar
-
atLineStart
private boolean atLineStart
-
prefixes
private final java.util.LinkedList<java.lang.String> prefixes
-
tight
private final java.util.LinkedList<java.lang.Boolean> tight
-
rawEscapes
private final java.util.LinkedList<CharMatcher> rawEscapes
-
-
Method Detail
-
raw
public void raw(java.lang.String s)
Write the supplied string (raw/unescaped except ifpushRawEscape(org.commonmark.text.CharMatcher)
was used).
-
raw
public void raw(char c)
Write the supplied character (raw/unescaped except ifpushRawEscape(org.commonmark.text.CharMatcher)
was used).
-
text
public void text(java.lang.String s, CharMatcher escape)
Write the supplied string with escaping.- Parameters:
s
- the string to writeescape
- which characters to escape
-
line
public void line()
Write a newline (line terminator).
-
block
public void block()
Enqueue a block separator to be written before the next text is written. Block separators are not written straight away because if there are no more blocks to write we don't want a separator (at the end of the document).
-
pushPrefix
public void pushPrefix(java.lang.String prefix)
Push a prefix onto the top of the stack. All prefixes are written at the beginning of each line, until the prefix is popped again.- Parameters:
prefix
- the raw prefix string
-
writePrefix
public void writePrefix(java.lang.String prefix)
Write a prefix.- Parameters:
prefix
- the raw prefix string to write
-
popPrefix
public void popPrefix()
Remove the last prefix from the top of the stack.
-
pushTight
public void pushTight(boolean tight)
Change whether blocks are tight or loose. Loose is the default where blocks are separated by a blank line. Tight is where blocks are not separated by a blank line. Tight blocks are used in lists, if there are no blank lines within the list.Note that changing this does not affect block separators that have already been enqueued with
block()
, only future ones.
-
popTight
public void popTight()
Remove the last "tight" setting from the top of the stack.
-
pushRawEscape
public void pushRawEscape(CharMatcher rawEscape)
Escape the characters matching the supplied matcher, in all text (text and raw). This might be useful to extensions that add another layer of syntax, e.g. the tables extension that uses `|` to separate cells and needs all `|` characters to be escaped (even in code spans).- Parameters:
rawEscape
- the characters to escape in raw text
-
popRawEscape
public void popRawEscape()
Remove the last raw escape from the top of the stack.
-
getLastChar
public char getLastChar()
- Returns:
- the last character that was written
-
isAtLineStart
public boolean isAtLineStart()
-
write
private void write(java.lang.String s, CharMatcher escape)
-
write
private void write(char c)
-
writePrefixes
private void writePrefixes()
-
flushBlockSeparator
private void flushBlockSeparator()
If a block separator has been enqueued withblock()
but not yet written, write it now.
-
append
private void append(char c, CharMatcher escape) throws java.io.IOException
- Throws:
java.io.IOException
-
isTight
private boolean isTight()
-
needsEscaping
private boolean needsEscaping(char c, CharMatcher escape)
-
rawNeedsEscaping
private boolean rawNeedsEscaping(char c)
-
-