Skip to content

Commit

Permalink
Added option to convert words back to plain text (#79)
Browse files Browse the repository at this point in the history
Fixes #75
  • Loading branch information
britzl authored Feb 10, 2023
1 parent 457db5b commit ca17297
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,16 @@ Removes all gui text nodes created by `richtext.create()`.
* `words` (table) - Table of words, as received from a call to `richtext.create()`.


### richtext.plaintext(words)
Returns the words created by `richtext.create()` as a plain text string without any formatting or tags. Linebreaks are included in the returned string.

**PARAMETERS**
* `words` (table) - Table of words, as received from a call to `richtext.create()`.

**RETURNS**
* `plaintext` (string) - Plain text version of the words.


### richtext.ALIGN_LEFT
Left-align text. The words of a line starts at the specified position (see `richtext.create` settings above).

Expand Down
1 change: 1 addition & 0 deletions example/example.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ local function create_complex_example()
local words, metrics = richtext.create(text, "Roboto", settings)

print("The text consists of " .. tostring(metrics.char_count) .. " characters")
print("The plain-text is " .. richtext.plaintext(words))

-- adjust background to cover text
gui.set_size(settings.parent, vmath.vector3(metrics.width, metrics.height, 0))
Expand Down
14 changes: 14 additions & 0 deletions richtext/richtext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -721,5 +721,19 @@ function M.remove(words)
end
end

function M.plaintext(words)
local s = ""
for i=1,#words do
local word = words[i]
if word.text then
s = s .. word.text
if word.linebreak then
s = s .. "\n"
end
end
end
return s
end


return M

0 comments on commit ca17297

Please sign in to comment.