Coder Social home page Coder Social logo

markup's Introduction

Markup encoding/decoding for Common Lisp

There are a lot of markup-esque languages out there (HTML, XML, ...). This is just a simple package that can be used by higher-level markup languages for encoding and decoding strings with tags and character/entity references in them for Common Lisp.

Quickstart

There are only 2 functions exported:

(markup-encode string)
(markup-decode string &key entities)

Encoding with markup-encode will properly convert all standardly encoded characters (e.g. &, <, >, ", and ') into their named entity. All other non-ASCII characters will encode into a character reference.

CL-USER > (markup-encode "<This & That>")
"&lt;This &amp; That&gt;"

Likewise, markup-decode will perform the inverse.

CL-USER > (markup-decode *)
"<This & That>"

Note: encoding and decoding will always return a new string, even if there is nothing to encode or decode.

The markup-decode function also takes an optional entities keyword argument. This allows for passing in of document-specific entities. There are already a plethora of common entities that are tested against when decoding, but (XML for example) some markup languages allow the user to define their own entities. If passed in, document entities take priority.

CL-USER > (markup-decode "&bull; Item 1.a. is &cool;" :entities '(("cool" . "AWESOME!")))
"• Item 1.a. is AWESOME!"

Customizing Entity References

Different markup languages use different predicates for what make up a valid entity reference character. You can override the predicates used with the special variables *markup-entity-start-char-p* and *markup-entity-char-p*.

Note: by default, these two variables default to alpha-char-p.

Simply set these to what you'd like them to be for your markup language and then decode the markup. For example:

(let ((*markup-entity-start-char-p* 'xml-name-char-p)
      (*markup-entity-char-p* 'xml-token-char-p))
  (markup-decode "&my-valid.xml:entity;"))

Preventing Infinite Expansion

Markup languages typically allow for recursive expansion of entities. For example, in XML:

<!DOCTYPE doc [
<!ENTITY a "&b;">
<!ENTITY b "&c;">
<!ENTITY c "Hello, world!">
]>

If you were to use &a; in your document, it would expand to "Hello, world!" appropriately. However, malformed entity references can be used to exploit code. Circular entity references can cause infinite loops, and grossly expanding entities can allow for out of memory crashes.

To help mitigate this, the *markup-entity-level* special variable allows you to cap the maximum level of expansion recursion that can take place. By default this is set to 3 - a good safe value.

That's it!

markup's People

Contributors

massung avatar

Watchers

Knut Olav Bøhmer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.