diff --git a/README.md b/README.md
index 353bac5..18ca824 100644
--- a/README.md
+++ b/README.md
@@ -45,7 +45,11 @@ The following tags are supported:
| | | `Foobar` |
| | | `Foobar` |
| font | Change font | `Foobar` |
-| img | Display image | `
` |
+| img | Display image | `
` |
+| br | Insert a line break (see notes on linebreak) | `
` |
+
+### Line breaks
+Note that there is no need for the HTML `
` tag since line breaks (i.e. `\n`) are parsed and presented by the system.
### Named colors
The following named colors are supported:
@@ -75,9 +79,6 @@ The following named colors are supported:
| white | `#ffffffff` |  |
| yellow | `#ffff00ff` |  |
-## Line breaks
-There is no need for the HTML `
` tag since line breaks (i.e. `\n`) are parsed and presented by the system.
-
# Usage
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:
@@ -99,7 +100,7 @@ The RichText library will create gui text nodes representing the markup in the t
color = vmath.vector4(0.95, 0.95, 1.0, 1.0),
}
- local text = "RichTextLorem ipsum
dolor sit amet, consectetur adipiscing elit. Nunc tincidunt mattis libero non viverra.\n\nNullam ornare
accumsan rhoncus.\n\nNunc placerat nibh a purus auctor, id scelerisque massa rutrum."
+ local text = "RichTextLorem ipsum
dolor sit amet, consectetur adipiscing elit. Nunc tincidunt mattis libero non viverra.\n\nNullam ornare
accumsan rhoncus.\n\nNunc placerat nibh a purus auctor, id scelerisque massa rutrum."
richtext.create(text, "Roboto", settings)
diff --git a/example/example.gui_script b/example/example.gui_script
index 9125485..d4f2f39 100644
--- a/example/example.gui_script
+++ b/example/example.gui_script
@@ -1,6 +1,6 @@
local richtext = require "richtext.richtext"
-local TEXT = "RichTextLorem ipsum
dolor sit amet, consectetur adipiscing elit. Nunc tincidunt mattis libero non viverra.\n\nNullam ornare
accumsan rhoncus.\n\nNunc placerat nibh a purus auctor, id scelerisque massa rutrum."
+local TEXT = "RichTextLorem ipsum
dolor sit amet, consectetur adipiscing elit.
Nunc tincidunt mattis libero non viverra.\n\nNullam ornare
accumsan rhoncus.\n\nNunc placerat nibh a purus auctor, id scelerisque massa rutrum."
function init(self)
local settings = {
diff --git a/richtext/parse.lua b/richtext/parse.lua
index 9ea41ff..676d6ed 100644
--- a/richtext/parse.lua
+++ b/richtext/parse.lua
@@ -14,6 +14,8 @@ local function parse_tag(tag, params)
settings.bold = true
elseif tag == "i" then
settings.italic = true
+ elseif tag == "br" then
+ settings.linebreak = true
elseif tag == "img" then
local texture, anim = params:match("(.-):(.*)")
settings.image = {
@@ -60,18 +62,29 @@ local function split_text(text, settings, words)
assert(text)
assert(settings)
assert(words)
+
+ -- special treatment of empty text with a linebreak
+ if text == "" and settings.linebreak then
+ add_word(text, settings, words)
+ return
+ end
+
+ -- the Lua pattern expects the text to have a linebreak at the end
local added_linebreak = false
if text:sub(-1)~="\n" then
added_linebreak = true
text = text .. "\n"
end
+ -- split into lines
for line in text:gmatch("(.-)\n") do
split_line(line, settings, words)
+ -- flag last word of a line as having a linebreak
local last = words[#words]
last.linebreak = true
end
+ -- remove the last linebreak if we manually added it above
if added_linebreak then
local last = words[#words]
last.linebreak = false
@@ -83,13 +96,19 @@ end
local function find_tag(text)
assert(text)
-- find tag, end if no tag was found
- local before_start_tag, tag, after_start_tag = text:match("(.-)(<[^/]%S->)(.*)")
+ local before_start_tag, tag, after_start_tag = text:match("(.-)(?%S->)(.*)")
if not before_start_tag or not tag or not after_start_tag then
return nil
end
-- parse the tag, split into name and optional parameters
- local name, params = tag:match("<(%a+)=?(%S*)>")
+ local name, params, empty = tag:match("<(%a+)=?(%S-)(/?)>")
+
+ -- empty tag, ie tag without content
+ -- example
and
+ if empty == "/" then
+ return before_start_tag, name, params, "", after_start_tag
+ end
-- find end tag
local inside_tag, after_end_tag = after_start_tag:match("(.-)" .. name .. ">(.*)")