Skip to content

Commit

Permalink
FEAT: bbcode csv table emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Dec 12, 2023
1 parent 8ee972c commit e395815
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 5 deletions.
73 changes: 69 additions & 4 deletions src/mezz/codec-bbcode.reb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ REBOL [
Name: bbcode
Type: module
Options: [delay]
Version: 0.3.1
Version: 0.3.2
Title: "Codec: BBcode"
Purpose: {Basic BBCode implementation. For more info about BBCode check http://en.wikipedia.org/wiki/BBCode}
File: https://raw.githubusercontent.com/Oldes/Rebol3/master/src/mezz/codec-bbcode.reb
Expand All @@ -13,7 +13,8 @@ REBOL [
0.2.0 19-Feb-2012 "review"
0.2.1 22-Aug-2012 "added [hr] and [anchor]"
0.3.0 24-Apr-2020 "ported to Rebol3"
0.3.1 11-Dec-2023 "FIX: `bbcode` must accept only string input"
0.3.1 11-Dec-2023 "FIX: `bbcode` must accept only string input"\
0.3.2 12-Dec-2023 "FEAT: csv table emitter"
]
]

Expand All @@ -39,6 +40,7 @@ ch_digits: charset [#"0" - #"9"]
ch_hexa: charset [#"a" - #"f" #"A" - #"F" #"0" - #"9"]
ch_name: charset [#"a" - #"z" #"A" - #"Z" #"*" #"0" - #"9"]
ch_url: charset [#"a" - #"z" #"A" - #"Z" #"0" - #"9" "./:~+-%#\_=&?@"]
ch_crlf: charset CRLF
ch_safe-value-chars: complement charset {'"}

rl_newline: [CRLF | LF]
Expand Down Expand Up @@ -188,6 +190,69 @@ emit-tag: func[tag][
insert tail html either block? tag [rejoin tag][tag]
]

emit-tag-csv: function/with [spec [string!]][
row: "" ;; no copy, it is cleared each time
trim/head/tail spec

close-p-if-possible
close-tags
emit-tag [{<table} form-attribute "class" form-attribute "align" form-attribute "style" {>^/}]
all [
widths: get-attribute "widths"
widths: transcode widths
]
if align: get-attribute "coltype" [
parse align [
some [
#"c" (emit-tag {<col align="center">^/})
| #"l" (emit-tag {<col align="left">^/})
| #"r" (emit-tag {<col align="right">^/})
| #"j" (emit-tag {<col align="justify">^/})
]
]
]
ch_divider: charset get-attribute/default "divider" TAB
ch_notDivider: complement union ch_divider ch_crlf
rl_data: [copy data any ch_notDivider]

data: align: none
row-num: col-num: col-width: 0
datatag: "th" ;; first row is always used for headers!
parse spec [
some [
(
clear row
++ row-num
)
any ch_space
some [
rl_data
1 ch_divider
(
append row ajoin [{<} datatag get-col-width {>} data {</} datatag {>}]
)
]
rl_data [rl_newline | end] (
append row ajoin [{<} datatag get-col-width {>} data {</} datatag {>}]
datatag: "td"
emit-tag ["<tr>" row "</tr>^/"]
)
]
]
emit-tag "</table>"
] [
data: widths: align: row-num: col-num: col-width: none
get-col-width: does [
++ col-num
either all [
row-num = 1
block? widths
col-width: pick widths col-num
integer? col-width
][ ajoin [" width=" col-width] ][ "" ]
]
]

enabled-tags: [
"b" "i" "s" "u" "del" "h1" "h2" "h3" "h4" "h5" "h6" "span" "class"
"ins" "dd" "dt" "ol" "ul" "li" "url" "list" "br" "hr"
Expand Down Expand Up @@ -414,8 +479,8 @@ bbcode: func [
]
unless empty? opened-tags [ close-tags ]
html
]
if error? err [
][
err: system/state/last-error
; send possible trimmed error in the result instead of throwing it!
append html ajoin ["^/#[ERROR! [code: " err/code " type: " err/type " id: " err/id #"]"]
]
Expand Down
21 changes: 20 additions & 1 deletion src/tests/units/bbcode-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ if find codecs 'BBCode [
{[hr10%]} {<p><hr style="width:10%"></p>}
{[anchor]foo[/anchor]} {<p><a name="foo"></a></p>}
{[class=underline]foo} {<p><span class="underline">foo</span></p>}
{[csv class=vysledky]
name number position
foo 1 2
boo 2 1
[/csv]} {<table class="vysledky">
<tr><th>name</th><th>number</th><th>position</th></tr>
<tr><td>foo</td><td>1</td><td>2</td></tr>
<tr><td>boo</td><td>2</td><td>1</td></tr>
</table>}

{[csv divider=';' align='center' coltype='lcr' widths='100 20 *']
name;number;position
[/csv]}
{<table align="center">
<col align="left">
<col align="center">
<col align="right">
<tr><th width=100>name</th><th width=20>number</th><th>position</th></tr>
</table>}
]
bbcode: :codecs/bbcode/decode
foreach [src result] test-cases [
Expand All @@ -90,7 +109,7 @@ if find codecs 'BBCode [
]
--test-- "bbcode with binary input"
src: {text [b]bold[/b] abc}
--assert (decode 'bbcode srt) = (decode 'bbcode to binary! src)
--assert (decode 'bbcode src) = (decode 'bbcode to binary! src)

===end-group===
]
Expand Down

0 comments on commit e395815

Please sign in to comment.