###* Definition of the Markdown to WordProcessingML conversion routine. @license MIT. Copyright (c) 2015 James Devlin / FluentDesk. @module utils/html-to-wpml ### _ = require 'underscore' HTML5Tokenizer = require 'simple-html-tokenizer' module.exports = ( html ) -> # Tokenize the HTML stream. tokens = HTML5Tokenizer.tokenize( html ) final = is_bold = is_italic = is_link = link_url = '' # Process , , and elements in the HTML stream, producing # equivalent WordProcessingML that can be dumped into a or other # text container element. _.each tokens, ( tok ) -> switch tok.type when 'StartTag' switch tok.tagName when 'p' then final += '' when 'strong' then is_bold = true when 'em' then is_italic = true when 'a' is_link = true; link_url = tok.attributes.filter((attr) -> attr[0] == 'href' )[0][1]; when 'EndTag' switch tok.tagName when 'p' then final += '' when 'strong' then is_bold = false when 'em' then is_italic = false when 'a' then is_link = false when 'Chars' if( tok.chars.trim().length ) style = if is_bold then '' else '' style += if is_italic then '' else '' style += if is_link then '' else '' final += (if is_link then ('') else '') + '' + style + '' + tok.chars + '' + (if is_link then '' else '') final