{h1: Text formatting, an experiment} Ever since I started writing Bitter, I've been wondering how to apply functional programming concepts to creating a better text formatter. Before continuing, make sure you have read my {a @href /bitter-syntax-highlighter: introduction to the Bitter syntax highlighter} as it contains some important ideas on which I will be elaborating in this article. For now I'm going to call this idea for a language 'Terror', as it's a text formatter and probably going to be full of errors. {h1: What would it look like?} Obviously the syntax cannot be identical to Bitter, as all Bitter needs to do is wrap pieces of text with 'tags'. Here, we actually need a way to define HTML. So I've introduced three new functions: {dl: {dt: {code: element(name)}} {dd: Define a new element} {dt: {code: attribute(name, value)}} {dd: Define a new attribute} {dt: {code: text(value)}} {dd: Define a new text node} } Each {code: name} or {code: value} accepts a string, possibly containing references to matched data: {code: attribute('class', '$1')}. {pre.language-php: } So, as you can see, this example looks for any Textile style headers, and creates elements to represent them. If you where to pass it this text: {pre: h1. Heading one h2. Heading two} Then you'd end up with this output: {pre.language-html:

Heading one

Heading two

} Pretty straight forward, huh? All you do is tell it to match a particular 'syntax' and what to output it as, everything else is automatic. {h1: It's not all easy} There's one obvious issue with this method: not enough logic. The example above assumes you want your headers to be as they where written in the text, there's no shortcut way to offset the header by one: {code: h1 + 1 = h2}. Instead you'd have to write individual rules for each of the possible states. Not an ideal solution in the least: {pre.language-php: } It may be possible to instead use XPath to match captures: {pre.language-php: } This poses it's own problems however, since there would be a considerable amount of processing needed to convert an array of matches into XML which can then be used to execute XPath expressions against. But then again, it'd be better than introducing yet another non-standard language. {h1: Bite me} So, that's what I'd like to create, when I have the time. My ideas are not perfectly brilliant, nor really perfect or brilliant, so please, share your thoughts.