Skip to content

Commit e395815

Browse files
committed
FEAT: bbcode csv table emitter
1 parent 8ee972c commit e395815

File tree

2 files changed

+89
-5
lines changed

2 files changed

+89
-5
lines changed

src/mezz/codec-bbcode.reb

+69-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ REBOL [
22
Name: bbcode
33
Type: module
44
Options: [delay]
5-
Version: 0.3.1
5+
Version: 0.3.2
66
Title: "Codec: BBcode"
77
Purpose: {Basic BBCode implementation. For more info about BBCode check http://en.wikipedia.org/wiki/BBCode}
88
File: https://raw.githubusercontent.com/Oldes/Rebol3/master/src/mezz/codec-bbcode.reb
@@ -13,7 +13,8 @@ REBOL [
1313
0.2.0 19-Feb-2012 "review"
1414
0.2.1 22-Aug-2012 "added [hr] and [anchor]"
1515
0.3.0 24-Apr-2020 "ported to Rebol3"
16-
0.3.1 11-Dec-2023 "FIX: `bbcode` must accept only string input"
16+
0.3.1 11-Dec-2023 "FIX: `bbcode` must accept only string input"\
17+
0.3.2 12-Dec-2023 "FEAT: csv table emitter"
1718
]
1819
]
1920

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

4446
rl_newline: [CRLF | LF]
@@ -188,6 +190,69 @@ emit-tag: func[tag][
188190
insert tail html either block? tag [rejoin tag][tag]
189191
]
190192

193+
emit-tag-csv: function/with [spec [string!]][
194+
row: "" ;; no copy, it is cleared each time
195+
trim/head/tail spec
196+
197+
close-p-if-possible
198+
close-tags
199+
emit-tag [{<table} form-attribute "class" form-attribute "align" form-attribute "style" {>^/}]
200+
all [
201+
widths: get-attribute "widths"
202+
widths: transcode widths
203+
]
204+
if align: get-attribute "coltype" [
205+
parse align [
206+
some [
207+
#"c" (emit-tag {<col align="center">^/})
208+
| #"l" (emit-tag {<col align="left">^/})
209+
| #"r" (emit-tag {<col align="right">^/})
210+
| #"j" (emit-tag {<col align="justify">^/})
211+
]
212+
]
213+
]
214+
ch_divider: charset get-attribute/default "divider" TAB
215+
ch_notDivider: complement union ch_divider ch_crlf
216+
rl_data: [copy data any ch_notDivider]
217+
218+
data: align: none
219+
row-num: col-num: col-width: 0
220+
datatag: "th" ;; first row is always used for headers!
221+
parse spec [
222+
some [
223+
(
224+
clear row
225+
++ row-num
226+
)
227+
any ch_space
228+
some [
229+
rl_data
230+
1 ch_divider
231+
(
232+
append row ajoin [{<} datatag get-col-width {>} data {</} datatag {>}]
233+
)
234+
]
235+
rl_data [rl_newline | end] (
236+
append row ajoin [{<} datatag get-col-width {>} data {</} datatag {>}]
237+
datatag: "td"
238+
emit-tag ["<tr>" row "</tr>^/"]
239+
)
240+
]
241+
]
242+
emit-tag "</table>"
243+
] [
244+
data: widths: align: row-num: col-num: col-width: none
245+
get-col-width: does [
246+
++ col-num
247+
either all [
248+
row-num = 1
249+
block? widths
250+
col-width: pick widths col-num
251+
integer? col-width
252+
][ ajoin [" width=" col-width] ][ "" ]
253+
]
254+
]
255+
191256
enabled-tags: [
192257
"b" "i" "s" "u" "del" "h1" "h2" "h3" "h4" "h5" "h6" "span" "class"
193258
"ins" "dd" "dt" "ol" "ul" "li" "url" "list" "br" "hr"
@@ -414,8 +479,8 @@ bbcode: func [
414479
]
415480
unless empty? opened-tags [ close-tags ]
416481
html
417-
]
418-
if error? err [
482+
][
483+
err: system/state/last-error
419484
; send possible trimmed error in the result instead of throwing it!
420485
append html ajoin ["^/#[ERROR! [code: " err/code " type: " err/type " id: " err/id #"]"]
421486
]

src/tests/units/bbcode-test.r3

+20-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ if find codecs 'BBCode [
7777
{[hr10%]} {<p><hr style="width:10%"></p>}
7878
{[anchor]foo[/anchor]} {<p><a name="foo"></a></p>}
7979
{[class=underline]foo} {<p><span class="underline">foo</span></p>}
80+
{[csv class=vysledky]
81+
name number position
82+
foo 1 2
83+
boo 2 1
84+
[/csv]} {<table class="vysledky">
85+
<tr><th>name</th><th>number</th><th>position</th></tr>
86+
<tr><td>foo</td><td>1</td><td>2</td></tr>
87+
<tr><td>boo</td><td>2</td><td>1</td></tr>
88+
</table>}
89+
90+
{[csv divider=';' align='center' coltype='lcr' widths='100 20 *']
91+
name;number;position
92+
[/csv]}
93+
{<table align="center">
94+
<col align="left">
95+
<col align="center">
96+
<col align="right">
97+
<tr><th width=100>name</th><th width=20>number</th><th>position</th></tr>
98+
</table>}
8099
]
81100
bbcode: :codecs/bbcode/decode
82101
foreach [src result] test-cases [
@@ -90,7 +109,7 @@ if find codecs 'BBCode [
90109
]
91110
--test-- "bbcode with binary input"
92111
src: {text [b]bold[/b] abc}
93-
--assert (decode 'bbcode srt) = (decode 'bbcode to binary! src)
112+
--assert (decode 'bbcode src) = (decode 'bbcode to binary! src)
94113

95114
===end-group===
96115
]

0 commit comments

Comments
 (0)