site

files for beauhilton.com
git clone https://git.beauhilton.com/site.git
Log | Files | Refs

escape-html.lua (933B)


      1 -- Escapes HTML special characters (<, >, &) in the content of elements
      2 -- matching a selector
      3 --
      4 -- Sample configuration that converts content of <pre> elements to its HTML source:
      5 -- [plugins.escape_html]
      6 --   file = "plugins/escape-html.lua"
      7 --
      8 -- [widgets.raw-html-in-pre]
      9 --   widget = "escape_html"
     10 --   selector = "pre"
     11 --
     12 -- Minimum soupault version: 1.6
     13 -- Author: Daniil Baturin
     14 -- License: MIT
     15 
     16 selector = config["selector"]
     17 if not selector then
     18   Plugin.fail("Missing required option \"selector\"")
     19 end
     20 
     21 function escape_html(element)
     22   content = HTML.inner_html(element)
     23   -- HTML.create_text escapes HTML special characters
     24   content = HTML.create_text(content)
     25   HTML.replace_content(element, content)
     26 end
     27 
     28 elements = HTML.select(page, selector)
     29 
     30 if not elements then
     31   Plugin.exit("No elements found, nothing to do")
     32 end
     33 
     34 local index = 1
     35 while elements[index] do
     36   escape_html(elements[index])
     37   index = index + 1
     38 end