This commit is contained in:
Daylin Morgan 2023-09-10 18:14:36 -05:00
parent b36de6c556
commit e6639f1669
Signed by: daylin
GPG Key ID: C1E52E7DD81DF79F
9 changed files with 149 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
*.out
tests/*
!tests/*.nims
!tests/*.nim

21
LICENSE Normal file
View File

@ -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.

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# bbansi

13
bbansi.nimble Normal file
View File

@ -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"

54
src/bbansi.nim Normal file
View File

@ -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"

27
src/bbansi/styles.nim Normal file
View File

@ -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)

1
tests/config.nims Normal file
View File

@ -0,0 +1 @@
switch("path", "$projectDir/../src")

18
tests/tbasic.nim Normal file
View File

@ -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"

8
todo.md Normal file
View File

@ -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
<!-- generated with <3 by daylinmorgan/todo -->