Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Underline and strikethrough #38

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/main/java/com/github/rjeschke/txtmark/Decorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,69 @@ public interface Decorator
* The StringBuilder to write to.
*/
public void closeImage(final StringBuilder out);

/**
* Called when a word is underlined.
*
* <p>
* Default implementation is:
* </p>
*
* <pre>
* <code>out.append("&lt;u&gt;\n");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void openUnderlined(final StringBuilder out);

/**
* Called when an underlined word is closed.
*
* <p>
* Default implementation is:
* </p>
*
* <pre>
* <code>out.append("&lt;/u&gt;\n");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void closeUnderlined(final StringBuilder out);

/**
* Called when an word is strikethroughed.
*
* <p>
* Default implementation is:
* </p>
*
* <pre>
* <code>out.append("&lt;s&gt;\n");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void openStrikethrough(final StringBuilder out);

/**
* Called when an strikethroughed word is closed.
*
* <p>
* Default implementation is:
* </p>
*
* <pre>
* <code>out.append("&lt;s&gt;\n");</code>
* </pre>
*
* @param out
* The StringBuilder to write to.
*/
public void closeStrikethrough(final StringBuilder out);

}
26 changes: 26 additions & 0 deletions src/main/java/com/github/rjeschke/txtmark/DefaultDecorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,30 @@ public void closeImage(final StringBuilder out)
{
out.append(" />");
}

/** @see com.github.rjeschke.txtmark.Decorator#openUnderlined(StringBuilder) */
@Override
public void openUnderlined(StringBuilder out)
{
out.append("<u>");
}
/** @see com.github.rjeschke.txtmark.Decorator#closeUnderlined(StringBuilder) */
@Override
public void closeUnderlined(StringBuilder out)
{
out.append("</u>");
}
/** @see com.github.rjeschke.txtmark.Decorator#openStrikethrough(StringBuilder) */
@Override
public void openStrikethrough(StringBuilder out)
{
out.append("<s>");

}
/** @see com.github.rjeschke.txtmark.Decorator#closeStrikethrough(StringBuilder) */
@Override
public void closeStrikethrough(StringBuilder out)
{
out.append("</s>");
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/github/rjeschke/txtmark/Emitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,36 @@ private int recursiveEmitLine(final StringBuilder out, final String in, final in
out.append(in.charAt(pos));
}
break;
case PLUS:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove the EM_START case?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry that change can be reverted

temp.setLength(0);
b = this.recursiveEmitLine(temp, in, pos + 1, mt);
if (b > 0)
{
this.config.decorator.openUnderlined(out);
out.append(temp);
this.config.decorator.closeUnderlined(out);
pos = b;
}
else
{
out.append(in.charAt(pos));
}
break;
case MINUS:
temp.setLength(0);
b = this.recursiveEmitLine(temp, in, pos + 1, mt);
if (b > 0)
{
this.config.decorator.openStrikethrough(out);
out.append(temp);
this.config.decorator.closeStrikethrough(out);
pos = b;
}
else
{
out.append(in.charAt(pos));
}
break;
case EM_STAR:
case EM_UNDERSCORE:
temp.setLength(0);
Expand Down Expand Up @@ -764,6 +794,10 @@ private MarkToken getToken(final String in, final int pos)

switch (c)
{
case '+':
return c0 != ' ' || c1 != ' ' ? MarkToken.PLUS : MarkToken.NONE;
case '-':
return c0 != ' ' || c1 != ' ' ? MarkToken.MINUS : MarkToken.NONE;
case '*':
if (c1 == '*')
{
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/github/rjeschke/txtmark/MarkToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ enum MarkToken
/** No token. */
NONE,
/** &#x2a; */
PLUS, // x+x
MINUS, // x-x
EM_STAR, // x*x
/** _ */
EM_UNDERSCORE, // x_x
Expand Down