Skip to content

Commit

Permalink
Added support for empty tags
Browse files Browse the repository at this point in the history
Also support for <br/> tag
  • Loading branch information
britzl committed Feb 28, 2018
1 parent 46fefb3 commit 2e1ba2f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ The following tags are supported:
| | | `<color=1.0,0.5,0,1.0>Foobar</color>` |
| | | `<color=#ff00ffff>Foobar</color>` |
| font | Change font | `<font=MyCoolFont>Foobar</font>` |
| img | Display image | `<img=texture:image></img>` |
| img | Display image | `<img=texture:image/>` |
| br | Insert a line break (see notes on linebreak) | `<br/>` |

### Line breaks
Note that there is no need for the HTML `<br/>` tag since line breaks (i.e. `\n`) are parsed and presented by the system.

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

## Line breaks
There is no need for the HTML `<br>` 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:

Expand All @@ -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 = "<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>"
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>"

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

Expand Down
2 changes: 1 addition & 1 deletion example/example.gui_script
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local richtext = require "richtext.richtext"

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

function init(self)
local settings = {
Expand Down
23 changes: 21 additions & 2 deletions richtext/parse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 <br/>
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
Expand All @@ -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 <br/> and <img=texture:image/>
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 .. ">(.*)")
Expand Down

0 comments on commit 2e1ba2f

Please sign in to comment.