Skip to content

Commit

Permalink
Set text replacement type to char instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Sep 11, 2024
1 parent 4b94640 commit 5cfcd6d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import eu.iamgio.quarkdown.visitor.node.NodeVisitor
/**
* A text-based symbol, such as `©`, `…`, `≥`.
* This is usually the result of a combination of multiple characters (e.g. `(C)` -> `©`).
* @param text processed symbol (e.g. `©`)
* @param symbol processed symbol (e.g. `©`)
* @see eu.iamgio.quarkdown.lexer.patterns.TextSymbolReplacement
*/
data class TextSymbol(override val text: String) : PlainTextNode {
data class TextSymbol(val symbol: Char) : PlainTextNode {
/**
* @return [symbol] as a string
*/
override val text: String
get() = symbol.toString()

override fun <T> accept(visitor: NodeVisitor<T>): T = visitor.visit(this)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,104 +5,106 @@ import eu.iamgio.quarkdown.lexer.tokens.TextSymbolToken

/**
* Patterns for sequences of characters that correspond to text symbols.
* @param result symbol that the sequence is replaced with
* @param regex regex pattern that matches the sequence to be replaced
*/
enum class TextSymbolReplacement(val result: String, val regex: Regex) {
enum class TextSymbolReplacement(val result: Char, val regex: Regex) {
/**
* `(C)` -> `©`
*/
COPYRIGHT("©", "\\(C\\)".toRegex()),
COPYRIGHT('©', "\\(C\\)".toRegex()),

/**
* `(R)` -> `®`
*/
REGISTERED("®", "\\(R\\)".toRegex()),
REGISTERED('®', "\\(R\\)".toRegex()),

/**
* `(TM)` -> `™`
*/
TRADEMARK("", "\\(TM\\)".toRegex()),
TRADEMARK('', "\\(TM\\)".toRegex()),

/**
* `-` -> `—`
*
* It must be surrounded by a word character and a space on both sides.
*/
EM_DASH("", "(?<=\\w\\s)-(?=\\s\\w)".toRegex()),
EM_DASH('', "(?<=\\w\\s)-(?=\\s\\w)".toRegex()),

/**
* `...` -> `…`
*
* Must be either at the beginning or end of a word, not in-between.
*/
ELLIPSIS("", "(\\.\\.\\.(?=\\s|\$))|((?<=\\s|^)\\.\\.\\.)".toRegex()),
ELLIPSIS('', "(\\.\\.\\.(?=\\s|\$))|((?<=\\s|^)\\.\\.\\.)".toRegex()),

/**
* `->` -> `→`
*/
SINGLE_RIGHT_ARROW("", "->".toRegex()),
SINGLE_RIGHT_ARROW('', "->".toRegex()),

/**
* `<-` -> `←`
*/
SINGLE_LEFT_ARROW("", "<-".toRegex()),
SINGLE_LEFT_ARROW('', "<-".toRegex()),

/**
* `=>` -> `⇒`
*/
DOUBLE_RIGHT_ARROW("", "=>".toRegex()),
DOUBLE_RIGHT_ARROW('', "=>".toRegex()),

/**
* `<==` -> `⇐`
*/
DOUBLE_LEFT_ARROW("", "<==".toRegex()),
DOUBLE_LEFT_ARROW('', "<==".toRegex()),

/**
* `>=` -> `≥`
*/
GREATER_EQUAL("", ">=".toRegex()),
GREATER_EQUAL('', ">=".toRegex()),

/**
* `<=` -> `≤`
*/
LESS_EQUAL("", "<=".toRegex()),
LESS_EQUAL('', "<=".toRegex()),

/**
* `!=` -> `≠`
*/
NOT_EQUAL("", "!=".toRegex()),
NOT_EQUAL('', "!=".toRegex()),

/**
* `+-` -> `±`
*/
PLUS_MINUS("±", "\\+-".toRegex()),
PLUS_MINUS('±', "\\+-".toRegex()),

/**
* `'` -> `‘`
*
* Must not be preceded by a word and must be followed by a word character.
*/
TYPOGRAPHIC_LEFT_APOSTROPHE("", "(?<=\\s|^)'(?=\\w)".toRegex()),
TYPOGRAPHIC_LEFT_APOSTROPHE('', "(?<=\\s|^)'(?=\\w)".toRegex()),

/**
* `'` -> `’`
*
* Must not be preceded by a whitespace.
*/
TYPOGRAPHIC_RIGHT_APOSTROPHE("", "(?<!\\s)'".toRegex()),
TYPOGRAPHIC_RIGHT_APOSTROPHE('', "(?<!\\s)'".toRegex()),

/**
* `"` -> `“`
*
* Must not be preceded by a word character and must be followed by a word character.
*/
TYPOGRAPHIC_LEFT_QUOTATION_MARK("", "(?<!\\w)\"(?=\\w)".toRegex()),
TYPOGRAPHIC_LEFT_QUOTATION_MARK('', "(?<!\\w)\"(?=\\w)".toRegex()),

/**
* `"` -> `”`
*
* Must not be preceded by a whitespace and not followed by a word character.
*/
TYPOGRAPHIC_RIGHT_QUOTATION_MARK("", "(?<!\\s)\"(?!\\w)".toRegex()),
TYPOGRAPHIC_RIGHT_QUOTATION_MARK('', "(?<!\\s)\"(?!\\w)".toRegex()),
;

/**
Expand Down

0 comments on commit 5cfcd6d

Please sign in to comment.