Skip to content

Commit 2e1ba2f

Browse files
committed
Added support for empty tags
Also support for <br/> tag
1 parent 46fefb3 commit 2e1ba2f

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ The following tags are supported:
4545
| | | `<color=1.0,0.5,0,1.0>Foobar</color>` |
4646
| | | `<color=#ff00ffff>Foobar</color>` |
4747
| font | Change font | `<font=MyCoolFont>Foobar</font>` |
48-
| img | Display image | `<img=texture:image></img>` |
48+
| img | Display image | `<img=texture:image/>` |
49+
| br | Insert a line break (see notes on linebreak) | `<br/>` |
50+
51+
### Line breaks
52+
Note that there is no need for the HTML `<br/>` tag since line breaks (i.e. `\n`) are parsed and presented by the system.
4953

5054
### Named colors
5155
The following named colors are supported:
@@ -75,9 +79,6 @@ The following named colors are supported:
7579
| white | `#ffffffff` | ![](https://placehold.it/15/ffffff/000000?text=+) |
7680
| yellow | `#ffff00ff` | ![](https://placehold.it/15/ffff00/000000?text=+) |
7781

78-
## Line breaks
79-
There is no need for the HTML `<br>` tag since line breaks (i.e. `\n`) are parsed and presented by the system.
80-
8182
# Usage
8283
The RichText library will create gui text nodes representing the markup in the text passed to the library. It will search for tags and split the entire text into words, where each word contains additional meta-data that is used to create and configure text nodes. This means that the library will create as many text nodes as there are words in the text. Example:
8384

@@ -99,7 +100,7 @@ The RichText library will create gui text nodes representing the markup in the t
99100
color = vmath.vector4(0.95, 0.95, 1.0, 1.0),
100101
}
101102

102-
local text = "<size=3>RichText</size>Lorem <color=0,0.5,0,1>ipsum </color><img=smileys:zombie></img> dolor <color=red>sit </color><color=#ff00ffff>amet, </color><size=1.15><font=Nanum>consectetur </font></size>adipiscing elit. <b>Nunc </b>tincidunt <b><i>mattis</i> libero</b> <i>non viverra</i>.\n\nNullam ornare <img=smileys:hungry></img>accumsan rhoncus.\n\nNunc placerat nibh a purus auctor, id scelerisque massa <size=2>rutrum.</size>"
103+
local text = "<size=3>RichText</size>Lorem <color=0,0.5,0,1>ipsum </color><img=smileys:zombie/> dolor <color=red>sit </color><color=#ff00ffff>amet, </color><size=1.15><font=Nanum>consectetur </font></size>adipiscing elit. <b>Nunc </b>tincidunt <b><i>mattis</i> libero</b> <i>non viverra</i>.\n\nNullam ornare <img=smileys:hungry/>accumsan rhoncus.\n\nNunc placerat nibh a purus auctor, id scelerisque massa <size=2>rutrum.</size>"
103104

104105
richtext.create(text, "Roboto", settings)
105106

example/example.gui_script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local richtext = require "richtext.richtext"
22

3-
local TEXT = "<size=3>RichText</size>Lorem <color=0,0.5,0,1>ipsum </color><img=smileys:zombie></img> dolor <color=red>sit </color><color=#ff00ffff>amet, </color><size=1.15><font=Nanum>consectetur </font></size>adipiscing elit. <b>Nunc </b>tincidunt <b><i>mattis</i> libero</b> <i>non viverra</i>.\n\nNullam ornare <img=smileys:hungry></img>accumsan rhoncus.\n\nNunc placerat nibh a purus auctor, id scelerisque massa <size=2>rutrum.</size>"
3+
local TEXT = "<size=3>RichText</size>Lorem <color=0,0.5,0,1>ipsum </color><img=smileys:zombie/> dolor <color=red>sit </color><color=#ff00ffff>amet, </color><size=1.15><font=Nanum>consectetur </font></size>adipiscing elit.<br/><b>Nunc </b>tincidunt <b><i>mattis</i> libero</b> <i>non viverra</i>.\n\nNullam ornare <img=smileys:hungry></img>accumsan rhoncus.\n\nNunc placerat nibh a purus auctor, id scelerisque massa <size=2>rutrum.</size>"
44

55
function init(self)
66
local settings = {

richtext/parse.lua

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ local function parse_tag(tag, params)
1414
settings.bold = true
1515
elseif tag == "i" then
1616
settings.italic = true
17+
elseif tag == "br" then
18+
settings.linebreak = true
1719
elseif tag == "img" then
1820
local texture, anim = params:match("(.-):(.*)")
1921
settings.image = {
@@ -60,18 +62,29 @@ local function split_text(text, settings, words)
6062
assert(text)
6163
assert(settings)
6264
assert(words)
65+
66+
-- special treatment of empty text with a linebreak <br/>
67+
if text == "" and settings.linebreak then
68+
add_word(text, settings, words)
69+
return
70+
end
71+
72+
-- the Lua pattern expects the text to have a linebreak at the end
6373
local added_linebreak = false
6474
if text:sub(-1)~="\n" then
6575
added_linebreak = true
6676
text = text .. "\n"
6777
end
6878

79+
-- split into lines
6980
for line in text:gmatch("(.-)\n") do
7081
split_line(line, settings, words)
82+
-- flag last word of a line as having a linebreak
7183
local last = words[#words]
7284
last.linebreak = true
7385
end
7486

87+
-- remove the last linebreak if we manually added it above
7588
if added_linebreak then
7689
local last = words[#words]
7790
last.linebreak = false
@@ -83,13 +96,19 @@ end
8396
local function find_tag(text)
8497
assert(text)
8598
-- find tag, end if no tag was found
86-
local before_start_tag, tag, after_start_tag = text:match("(.-)(<[^/]%S->)(.*)")
99+
local before_start_tag, tag, after_start_tag = text:match("(.-)(</?%S->)(.*)")
87100
if not before_start_tag or not tag or not after_start_tag then
88101
return nil
89102
end
90103

91104
-- parse the tag, split into name and optional parameters
92-
local name, params = tag:match("<(%a+)=?(%S*)>")
105+
local name, params, empty = tag:match("<(%a+)=?(%S-)(/?)>")
106+
107+
-- empty tag, ie tag without content
108+
-- example <br/> and <img=texture:image/>
109+
if empty == "/" then
110+
return before_start_tag, name, params, "", after_start_tag
111+
end
93112

94113
-- find end tag
95114
local inside_tag, after_end_tag = after_start_tag:match("(.-)</" .. name .. ">(.*)")

0 commit comments

Comments
 (0)