Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- a simple implementation of a basic push/pop stack
- list_stack = {
- _items = {},
- push = function(self, item)
- table.insert(self._items, item)
- end,
- pop = function(self)
- return table.remove(self._items)
- end,
- -- the top item in the stack will tell us which kind of list we are in right now
- get_top = function(self)
- return self._items[#self._items]
- end;
- }
- -- push "unordered" unto the top of the stack
- function ul()
- x = x + font.li:getWidth "\t"
- list_stack:push{"unordered"}
- end
- -- reverse the operations of ul() and
- -- pop the stack and raise an error if you're closing with the wrong kind of list
- function popul()
- x = x - font.li:getWidth "\t"
- local popped_mode = list_stack:pop()
- assert(popped_mode[1] == "unordered", "Trying to close a <ol> with </ul>")
- end
- -- push "ordered", 0, unto the top of the stack
- function ol()
- x = x + font.li:getWidth "\t"
- list_stack:push{"ordered",0}
- end
- -- reverse the operations of ol() and
- -- pop the stack and raise an error if you're closing with the wrong kind of list
- function popol()
- x = x - font.li:getWidth "\t"
- local popped_mode = list_stack:pop()
- assert(popped_mode[1] == "ordered", "Trying to close a <ul> with </ol>")
- end
- function li(text)
- local width, lines = font.li:getWrap(text, wrap or love.graphics.getWidth())
- local height = font.li:getLineHeight() * font.p:getHeight()
- love.graphics.setFont(font.li)
- -- notice how instead of just looking at a simple variable,
- -- we're looking at the top of the stack. That way, once you close a list,
- -- the previously opened one will take effect
- -- I also added a check to make sure you're using <li> inside either <ul> or <ol>
- local list_top = list_stack:get_top()
- if list_top[1] == "ordered" then
- list_top[2] = list_top[2] + 1
- love.graphics.print(list_top[2], x, y)
- elseif list_top[1] == "unordered" then
- love.graphics.print("•", x, y)
- else
- error("Trying to use <li> without matching <ul> or <ol>")
- end
- local x = x + font.li:getWidth"\t"
- for i = 1, #lines do
- love.graphics.print(lines[i], x, y)
- y = y + height
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement