Skip to content

Commit f94a23b

Browse files
committed
Add support for repeat tag
Fixes #56
1 parent 348061b commit f94a23b

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ The following tags are supported:
5454
| p | Adds extra spacing below the line where this | `<p>A paragraph</p>\nSome other text` |
5555
| | tag ends. Adds a newline before its opening | `<p=2.5>This has 2.5 lines of spacing<p>` |
5656
| | tag if it doesn't already exist. | |
57+
| repeat | Repeat the text enclosed by the tag | `<repeat=5>Echo </repeat> five times` |
5758

5859
### Line breaks
5960
Note that there is no need for the HTML `<br/>` tag since line breaks (i.e. `\n`) are parsed and presented by the system. Note that a single `<br>` (ie without a closing or empty tag) isn't supported (even though most browsers accept it).

example/example.gui_script

+22-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ local color = require "richtext.color"
33

44
local sherlock = [[At three o'clock precisely I was at Baker Street, but Holmes had not yet returned. The landlady informed me that he had left the house shortly after eight o'clock in the morning. I sat down beside the fire, however, with the intention of awaiting him, however long he might be. I was already deeply interested in his inquiry, for, though it was surrounded by none of the grim and strange features which were associated with the two crimes which I have already recorded, still, the nature of the case and the exalted station of his client gave it a character of its own. Indeed, apart from the nature of the investigation which my friend had on hand, there was something in his masterly grasp of a situation, and his keen, incisive reasoning, which made it a pleasure to me to study his system of work, and to follow the quick, subtle methods by which he disentangled the most inextricable mysteries. So accustomed was I to his invariable success that the very possibility of his failing had ceased to enter into my head. It was close upon four before the door opened, and a drunken-looking groom, ill-kempt and side-whiskered, with an inflamed face and disreputable clothes, walked into the room. Accustomed as I was to my friend's amazing powers in the use of disguises, I had to look three times before I was certain that it was indeed he. With a nod he vanished into the bedroom, whence he emerged in five minutes tweed-suited and respectable, as of old. Putting his hands into his pockets, he stretched out his legs in front of the fire and laughed heartily for some minutes. 'Well, really!' he cried, and then he choked and laughed again until he was obliged to lie back, limp and helpless, in the chair. 'What is it?' 'It's quite too funny. I am sure you could never guess how I employed my morning, or what I ended by doing.' 'I can't imagine. I suppose that you have been watching the habits, and perhaps the house, of Miss Irene Adler.' 'Quite so; but the sequel was rather unusual. I will tell you, however. I left the house a little after eight o'clock this morning in the character of a groom out of work. There is a wonderful sympathy and freemasonry among horsey men. Be one of them, and you will know all that there is to know. I soon found Briony Lodge. It is a bijou villa, with a garden at the back, but built out in front right up to the road, two stories. Chubb lock to the door. Large sitting-room on the right side, well furnished, with long windows almost to the floor, and those preposterous English window fasteners which a child could open. Behind there was nothing remarkable, save that the passage window could be reached from the top of the coach-house. I walked round it and examined it closely from every point of view, but without noting anything else of interest. 'I then lounged down the street and found, as I expected, that there was a mews in a lane which runs down by one wall of the garden. I lent the ostlers a hand in rubbing down their horses, and received in exchange twopence, a glass of half and half, two fills of shag tobacco, and as much information as I could desire about Miss Adler, to say nothing of half a dozen other people in the neighbourhood in whom I was not in the least interested, but whose biographies I was compelled to listen to.']]
55

6+
local ROBOTO = {
7+
regular = hash("Roboto-Regular"),
8+
italic = hash("Roboto-Italic"),
9+
bold = hash("Roboto-Bold"),
10+
bold_italic = hash("Roboto-BoldItalic"),
11+
}
12+
13+
local NANUM = {
14+
regular = hash("Nanum-Regular"),
15+
}
16+
17+
618
local function merge(...)
719
local out = {}
820
for _,words in pairs({...}) do
@@ -27,15 +39,8 @@ end
2739
local function create_complex_example()
2840
local settings = {
2941
fonts = {
30-
Roboto = {
31-
regular = hash("Roboto-Regular"),
32-
italic = hash("Roboto-Italic"),
33-
bold = hash("Roboto-Bold"),
34-
bold_italic = hash("Roboto-BoldItalic"),
35-
},
36-
Nanum = {
37-
regular = hash("Nanum-Regular"),
38-
},
42+
Roboto = ROBOTO,
43+
Nanum = NANUM,
3944
},
4045
layers = {
4146
fonts = {
@@ -204,12 +209,7 @@ end
204209
local function create_overlapping_tags_example()
205210
local settings_overlap = {
206211
fonts = {
207-
Roboto = {
208-
regular = hash("Roboto-Regular"),
209-
italic = hash("Roboto-Italic"),
210-
bold = hash("Roboto-Bold"),
211-
bold_italic = hash("Roboto-BoldItalic"),
212-
},
212+
Roboto = ROBOTO
213213
},
214214
position = vmath.vector3(0, 650, 0),
215215
}
@@ -231,7 +231,6 @@ local function create_html_entities_example()
231231

232232
local settings_zwsp = { position = vmath.vector3(20, 500, 0), align = richtext.ALIGN_LEFT, width = 640 }
233233
local zwsp = richtext.create("There is space&zwsp;&zwsp;&zwsp;&zwsp;here but you barely see it\nThere is no spacehere", "Roboto-Regular", settings_zwsp)
234-
print(#zwsp)
235234
return merge(ltgt, nbsp, zwsp)
236235
end
237236

@@ -299,6 +298,12 @@ local function create_colors_example()
299298
end
300299

301300

301+
local function create_repeat_example()
302+
local settings = { position = vmath.vector3(320, 450, 0), align = richtext.ALIGN_CENTER, fonts = { Roboto = ROBOTO } }
303+
return richtext.create("Now repeat<repeat=7>\n<b>after <color=red><i>me</i></color></b></repeat>\nWell done!", "Roboto", settings)
304+
end
305+
306+
302307
function init(self)
303308
msg.post(".", "acquire_input_focus")
304309

@@ -322,6 +327,7 @@ function init(self)
322327
{ name = "FADE IN", fn = create_fade_in_example },
323328
{ name = "PARAGRAPHS", fn = create_paragraph_example },
324329
{ name = "COLORS", fn = create_colors_example },
330+
{ name = "REPEAT", fn = create_repeat_example },
325331
}
326332

327333
self.back = gui.get_node("back/bg")

richtext/parse.lua

+21-11
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,30 @@ function M.parse(text, default_settings)
175175
merge_tags(empty_tag_settings, word_settings)
176176
add_word("", empty_tag_settings, all_words)
177177
elseif not is_endtag then
178-
-- open tag - parse and add it
179-
local tag_settings = parse_tag(name, params)
180-
open_tags[#open_tags + 1] = tag_settings
178+
if name == "repeat" then
179+
local text_to_repeat = after_tag:match("(.*)</repeat>")
180+
local repetitions = tonumber(params)
181+
if repetitions > 1 then
182+
after_tag = text_to_repeat:rep(repetitions - 1) .. after_tag
183+
end
184+
else
185+
-- open tag - parse and add it
186+
local tag_settings = parse_tag(name, params)
187+
open_tags[#open_tags + 1] = tag_settings
188+
end
181189
else
182-
-- end tag - remove it from the list of open tags
183-
local found = false
184-
for i=#open_tags,1,-1 do
185-
if open_tags[i].tag == name then
186-
table.remove(open_tags, i)
187-
found = true
188-
break
190+
if name ~= "repeat" then
191+
-- end tag - remove it from the list of open tags
192+
local found = false
193+
for i=#open_tags,1,-1 do
194+
if open_tags[i].tag == name then
195+
table.remove(open_tags, i)
196+
found = true
197+
break
198+
end
189199
end
200+
if not found then print(("Found end tag '%s' without matching start tag"):format(name)) end
190201
end
191-
if not found then print(("Found end tag '%s' without matching start tag"):format(name)) end
192202
end
193203

194204
if name == "p" then

0 commit comments

Comments
 (0)