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

Print markdown symbols in IRC for inline code blocks, bold, etc. #83

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,23 @@ class IrcRenderer(context: TextContentNodeRendererContext) : AbstractVisitor(),
}

override fun visit(code: Code?) {
textContent.write("`")
textContent.write(IrcFormattingCodes.MONOSPACE.char)
textContent.write(code?.literal)
textContent.write(IrcFormattingCodes.MONOSPACE.char)
textContent.write("`")
}

override fun visit(document: Document?) {
visitChildren(document)
}

override fun visit(emphasis: Emphasis?) {
textContent.write("*")
textContent.write(IrcFormattingCodes.ITALICS.char)
visitChildren(emphasis)
textContent.write(IrcFormattingCodes.ITALICS.char)
textContent.write("*")
}

override fun visit(fencedCodeBlock: FencedCodeBlock?) {
Expand Down Expand Up @@ -213,14 +217,16 @@ class IrcRenderer(context: TextContentNodeRendererContext) : AbstractVisitor(),
}

override fun visit(strongEmphasis: StrongEmphasis?) {
val wrapper: Char = when (strongEmphasis?.openingDelimiter) {
DiscordFormattingCodes.BOLD.code -> IrcFormattingCodes.BOLD.char
DiscordFormattingCodes.UNDERLINE.code -> IrcFormattingCodes.UNDERLINE.char
val (wrapper, marker) = when (strongEmphasis?.openingDelimiter) {
DiscordFormattingCodes.BOLD.code -> Pair(IrcFormattingCodes.BOLD.char, "**")
DiscordFormattingCodes.UNDERLINE.code -> Pair(IrcFormattingCodes.UNDERLINE.char, "__")
else -> throw IllegalArgumentException("Unknown strong emphasis delimiter: ${strongEmphasis?.openingDelimiter}")
}
textContent.write(marker)
textContent.write(wrapper)
visitChildren(strongEmphasis)
textContent.write(wrapper)
textContent.write(marker)
}

override fun visit(text: Text?) {
Expand All @@ -229,19 +235,23 @@ class IrcRenderer(context: TextContentNodeRendererContext) : AbstractVisitor(),

override fun visit(customBlock: CustomBlock?) {
if (customBlock is DiscordSpoiler) {
textContent.write("||")
textContent.write(spoilerFormatCodeSequence)
visitChildren(customBlock)
textContent.write(IrcFormattingCodes.COLOR.char)
textContent.write("||")
} else {
throw IllegalArgumentException("Unknown custom block: $customBlock")
}
}

override fun visit(customNode: CustomNode?) {
if (customNode is Strikethrough) {
textContent.write("~~")
textContent.write(IrcFormattingCodes.STRIKETHROUGH.char)
visitChildren(customNode)
textContent.write(IrcFormattingCodes.STRIKETHROUGH.char)
textContent.write("~~")
} else {
throw IllegalArgumentException("Unknown custom node: $customNode")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class TranslateFormattingTest {

@Test
fun discordToIrc() {
this.testDiscordToIrc("${Format.BOLD}Test bold${Format.BOLD}", "**Test bold**")
this.testDiscordToIrc("${Format.ITALIC}Test italics${Format.ITALIC}", "*Test italics*")
this.testDiscordToIrc("${Format.UNDERLINE}Test underlines${Format.UNDERLINE}", "__Test underlines__")
this.testDiscordToIrc("${IrcFormattingCodes.STRIKETHROUGH}Test strikethrough${IrcFormattingCodes.STRIKETHROUGH}", "~~Test strikethrough~~")
this.testDiscordToIrc("${Format.COLOR_CHAR}${IrcColorCodes.BLACK},${IrcColorCodes.BLACK}Test spoiler${Format.COLOR_CHAR}", "||Test spoiler||")
this.testDiscordToIrc("${IrcFormattingCodes.MONOSPACE}Test inline code${IrcFormattingCodes.MONOSPACE}", "`Test inline code`")
this.testDiscordToIrc("**${Format.BOLD}Test bold${Format.BOLD}**", "**Test bold**")
this.testDiscordToIrc("*${Format.ITALIC}Test italics${Format.ITALIC}*", "*Test italics*")
this.testDiscordToIrc("__${Format.UNDERLINE}Test underlines${Format.UNDERLINE}__", "__Test underlines__")
this.testDiscordToIrc("~~${IrcFormattingCodes.STRIKETHROUGH}Test strikethrough${IrcFormattingCodes.STRIKETHROUGH}~~", "~~Test strikethrough~~")
this.testDiscordToIrc("||${Format.COLOR_CHAR}${IrcColorCodes.BLACK},${IrcColorCodes.BLACK}Test spoiler${Format.COLOR_CHAR}||", "||Test spoiler||")
this.testDiscordToIrc("`${IrcFormattingCodes.MONOSPACE}Test inline code${IrcFormattingCodes.MONOSPACE}`", "`Test inline code`")

//this.testDiscordToIrc("Attached${Format.COLOR_CHAR}${IrcColorCodes.BLACK},${IrcColorCodes.BLACK}together${Format.COLOR_CHAR}", "Attached||together||") // TODO - fix bug in case
this.testDiscordToIrc("¯\\_(ツ)_/¯", "¯\\_(ツ)_/¯")
Expand Down