diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7fc161f --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.out +tests/* +!tests/*.nims +!tests/*.nim + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..971ec81 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 daylin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..903b682 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# bbansi + diff --git a/bbansi.nimble b/bbansi.nimble new file mode 100644 index 0000000..c5fc4bb --- /dev/null +++ b/bbansi.nimble @@ -0,0 +1,13 @@ +# Package + +version = "0.1.0" +author = "Daylin Morgan" +description = "bbcode -> ansi" +license = "MIT" +srcDir = "src" +installExt = @["nim"] + + +# Dependencies + +requires "nim >= 1.6.12" diff --git a/src/bbansi.nim b/src/bbansi.nim new file mode 100644 index 0000000..1df650b --- /dev/null +++ b/src/bbansi.nim @@ -0,0 +1,54 @@ +# This is just an example to get you started. A typical hybrid package +# uses this file as the main entry point of the application. +import std/[os, strutils] +import bbansi/styles + +# TODO: add support for some kind of FORCE_COLOR +let noColor = os.getEnv("NO_COLOR") != "" + + +proc bb*(s: string): string = + ## convert bbcode markup to ansi escape codes + var + i = 0 + addReset = false + pattern = "" + preChar = ' ' + + while i < s.len: + # start extracting pattern when you see '[' but not '[[' + if s[i] == '[' and preChar notin {'\\','['} and s[i+1] != '[': + inc i + while i < s.len and s[i] != ']': + preChar = s[i] + pattern.add s[i] + inc i + + if noColor: + inc i + continue + if pattern == "reset": + result.add bbReset + addReset = false + else: + for style in pattern.splitWhitespace(): + if style in codeMap: + addReset = true + result.add "\e[" & codeMap[style] & "m" + pattern = "" + inc i + else: + preChar = s[i] + result.add s[i] + inc i + + if addReset: + result.add "\e[0m" + +when isMainModule: + echo bb"[bold]bold" + echo bb"[red]red" + echo bb"[bold red]bold red" + echo bb"[bold red]bold red[reset] no more red" + echo bb"[unknown]this text is red no?" + echo bb"\[red] <- not a pattern [[red] <- also not a pattern" diff --git a/src/bbansi/styles.nim b/src/bbansi/styles.nim new file mode 100644 index 0000000..4183fcb --- /dev/null +++ b/src/bbansi/styles.nim @@ -0,0 +1,27 @@ + +import strtabs + +export strtabs + +let bbReset* ="\e[0m" + +let + codeMap* = { + "reset":"0", + "bold": "1", + "faint": "2", + "italic":"3", + "underline":"4", + "blink":"5", + "reverse":"7", + "conceal":"8", + "strike":"9", + "black":"30", + "red": "31", + "green":"32", + "yellow":"33", + "blue":"34", + "magenta":"35", + "cyan":"36", + "white":"37", + }.newStringTable(modeCaseInsensitive) diff --git a/tests/config.nims b/tests/config.nims new file mode 100644 index 0000000..80091ff --- /dev/null +++ b/tests/config.nims @@ -0,0 +1 @@ +switch("path", "$projectDir/../src") diff --git a/tests/tbasic.nim b/tests/tbasic.nim new file mode 100644 index 0000000..ddc0d05 --- /dev/null +++ b/tests/tbasic.nim @@ -0,0 +1,18 @@ +# This is just an example to get you started. You may wish to put all of your +# tests into a single file, or separate them into multiple `test1`, `test2` +# etc. files (better names are recommended, just make sure the name starts with +# the letter 't'). +# +# To run these tests, simply execute `nimble test`. + +import unittest + +import bbansi +test "basic": + check "\e[31mRed Text\e[0m" == bb"[red]Red Text" + check "No Style" == bb"No Style" + check "Unknown Style" == bb"[unknown]Unknown Style" + check "\e[1m\e[31mBold Red Text\e[0m" == bb"[bold red]Bold Red Text" + check "\e[1m\e[31mBold Red Text\e[0mPlain Text" == bb"[bold red]Bold Red Text[reset]Plain Text" + # not sure how rich handles this + check "[red] ignored pattern" == bb"[[red] ignored pattern" diff --git a/todo.md b/todo.md new file mode 100644 index 0000000..fe9bb86 --- /dev/null +++ b/todo.md @@ -0,0 +1,8 @@ +# bbansi todo's + +- [ ] use an actual type and `$` mechanism +- [ ] add better escaping mechanism for bracketed text +- [ ] add support for custom 1-256 numbers and 8-bit numbers and truecolor +- [ ] add binary to evaluate strings/show test card + +