/* * docCodeEnhancer.aus - Enhances doc page code blocks with CodeView. * * Strips HTML wrapper tags and entities, then replaces plain pre/code * blocks with syntax-highlighted CodeView blocks. Handles errors * gracefully so a failing code block does not break the whole page. */ class DocCodeEnhancer { /** * Enhances all pre/code blocks in the container with CodeView. * @p container is the DOM element containing the doc content. * @p productName is the product name string. */ public enhance(container) { preElements = container.getByTagName('pre'); preList = []; for (pre : preElements) { preList.add(pre); } for (pre : preList) { try { codeText = this.cleanCodeText(pre.getHtml()); cv = new CodeView(null, false, false, false); rendered = cv.render(codeText); container.replaceChild(rendered, pre); } catch(err) { /* If highlighting fails, leave the original pre block */ c.log('CodeView skipped block: ' + err); } } } /** * Strips wrapper tags and decodes HTML entities. * @p rawHtml is the raw innerHTML from a pre element. * @r A clean code string ready for the Highlighter. */ public cleanCodeText(string rawHtml) { /* Strip and wrapper tags */ text = rawHtml.replace('', ''); text = text.replace('', ''); /* Also handle variants */ idx = text.indexOf('= 0) { endIdx = text.indexOfStart('>', idx); if (endIdx >= 0) { tag = text.substr(idx, endIdx + 1); text = text.replace(tag, ''); } idx = text.indexOf(''); text = text.replace('"', '"'); text = text.replace(''', "'"); return text; } }