Skip to content

Commit ca17297

Browse files
authored
Added option to convert words back to plain text (#79)
Fixes #75
1 parent 457db5b commit ca17297

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,16 @@ Removes all gui text nodes created by `richtext.create()`.
318318
* `words` (table) - Table of words, as received from a call to `richtext.create()`.
319319

320320

321+
### richtext.plaintext(words)
322+
Returns the words created by `richtext.create()` as a plain text string without any formatting or tags. Linebreaks are included in the returned string.
323+
324+
**PARAMETERS**
325+
* `words` (table) - Table of words, as received from a call to `richtext.create()`.
326+
327+
**RETURNS**
328+
* `plaintext` (string) - Plain text version of the words.
329+
330+
321331
### richtext.ALIGN_LEFT
322332
Left-align text. The words of a line starts at the specified position (see `richtext.create` settings above).
323333

example/example.gui_script

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ local function create_complex_example()
6969
local words, metrics = richtext.create(text, "Roboto", settings)
7070

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

7374
-- adjust background to cover text
7475
gui.set_size(settings.parent, vmath.vector3(metrics.width, metrics.height, 0))

richtext/richtext.lua

+14
Original file line numberDiff line numberDiff line change
@@ -721,5 +721,19 @@ function M.remove(words)
721721
end
722722
end
723723

724+
function M.plaintext(words)
725+
local s = ""
726+
for i=1,#words do
727+
local word = words[i]
728+
if word.text then
729+
s = s .. word.text
730+
if word.linebreak then
731+
s = s .. "\n"
732+
end
733+
end
734+
end
735+
return s
736+
end
737+
724738

725739
return M

0 commit comments

Comments
 (0)