Created
June 13, 2011 08:29
-
-
Save niklasl/1022462 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require 'fs' | |
{print} = require 'util' | |
jsdom = require 'jsdom' | |
jquery = require 'jquery' | |
convert = (base=null) -> | |
$ = @$ | |
if not $('article').length | |
@document.innerHTML = "<article>#{@document.innerHTML}</article>" | |
partId = $('h1').text().toLowerCase() | |
$('article').attr('class', 'testpart') | |
.attr('id', partId) | |
if base | |
$('img').each -> | |
it = $(this) | |
it.attr('src', base + it.attr('src')) | |
$('*').each -> | |
it = $(this) | |
it.removeAttr('xml:lang') | |
it.removeClass('generated-style generated-style-2') | |
$('span').each -> | |
it = $(this) | |
if it.is('.no-style-override-1, .no-style-override-20') or | |
not it.attr('class') | |
it.replaceWith(it.html()) | |
['.x-fr-ge-rubrik, .x-fr-gerubrik2'].forEach (sel) -> | |
$(sel).each -> | |
it = $(this) | |
it.replaceWith("<h2>#{it.html()}</h2>") | |
['.x-rubrik', '.x-rubrik2'].forEach (sel) -> | |
$(sel).each -> | |
it = $(this) | |
parent = it.parent() | |
it.replaceWith """<h2>#{it.html()}</h2>""" | |
if it.is(':last') | |
parent.replaceWith """<section class="taskgroup">#{parent.html()}</section>""" | |
$('section.taskgroup').each -> | |
it = $(this) | |
it.nextUntil('section.taskgroup').detach().appendTo(it) | |
['p.x-fr-ga', 'p.x-bjudord'].forEach (sel) -> | |
$(sel).each -> | |
it = $(this) | |
parent = it.parent() | |
div = $('<div class="task"/>') | |
div.insertBefore(it) | |
it.nextUntil(sel).detach().appendTo(div) | |
it.detach().prependTo(div) | |
parent.addClass('group') | |
$('div').each -> | |
div = $(this) | |
return if div.is '.group' | |
number = altGroup = altList = undefined | |
inAlts = false | |
$('p', div).each () -> | |
it = $(this) | |
it.html($.trim(it.html())) | |
if it.is('p.x-fr-ga-xyz-o-kva, p.x-fr-ga, p.x-bjudord') | |
it.attr('class', 'description') | |
it.contents().filter( -> @nodeType == 3 ).first().each -> | |
tnode = $(this) | |
m = tnode.text().match(/^(\d+)\.(.*)/) | |
number = m[1] | |
tnode.replaceWith("""<span class="number">#{number}.</span>""" + m[2]) | |
inAlts = false | |
else if not inAlts and it.is('.x-p-st-enden-kva, | |
.x-p-st-enden-kva-utan-fraga, | |
.x-p-st-enden-kva, .x-p-st-enden-kva-override, | |
.x-p-st-enden-nog') | |
it.attr('class', 'assertion') | |
if not number | |
$('span', it).first().each -> | |
it = $(this) | |
number = it.text().match(/^(\d+)\./)[1] | |
it.attr('class', 'number') | |
else if it.is('.x-tillr-cklig') | |
it.attr('class', '') | |
else if altList != undefined # slurp rest into this | |
inAlts = true | |
altLetter = it.text().match(/^\s*(\w+)/)[1] | |
altId = 'alt-' + altGroup + '-' + altLetter | |
altList.append("""<li> | |
<input type="radio" name="choice-#{altGroup}" | |
value="#{altLetter}" id="#{altId}" /> | |
<label for="#{altId}">#{it.detach().html()}</label> | |
</li>""") | |
if number and altList == undefined | |
altGroup = partId + '-' + number | |
altList = $('<ul class="alternatives"/>').appendTo(div) | |
if number and altList | |
div.addClass('task') | |
div.attr('id', 'task-' + altGroup) | |
div.after '\n' | |
path = process.argv[2] | |
file = fs.readFileSync(path, 'utf-8') | |
window = jsdom.jsdom(null, null).createWindow() | |
jquery.create(window) | |
window.document.innerHTML = file | |
convert.call window, "/hp/2011/ovn/" | |
html = window.document.outerHTML | |
.replace /<(input|img|br)([^>]*)>/g, "<$1$2 />" | |
.replace /\r/g, '' | |
.replace /\n\s+\n/g, '\n' | |
print html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment