diff --git a/src/converter.js b/src/converter.js index 02e98b4..3f4b1d6 100644 --- a/src/converter.js +++ b/src/converter.js @@ -8,8 +8,8 @@ topNode.children().first().children('[text=Content]').length == 0) { topNode = topNode.children().first(); } - var bodyNode = new Outline(topNode, siteTitle, new HtmlGenerator(), '', '', 'html', []); - var htmlPages = bodyNode.process(); + var bodyNode = new Outline(topNode, '', '', 'html', []); + var htmlPages = bodyNode.process(siteTitle, new HtmlGenerator()); var zip = new JSZip(); htmlPages.forEach(function (page) { diff --git a/src/outline.js b/src/outline.js index dcedd48..0fa8532 100644 --- a/src/outline.js +++ b/src/outline.js @@ -1,4 +1,4 @@ -var Outline = function (node, siteTitle, generator, title, fileName, filePath, parentNavigationObject) { +var Outline = function (node, title, fileName, filePath, parentNavigationObject) { var navigationObject = parentNavigationObject.map(function (navBar) { return navBar.map(function (link) { return { @@ -11,7 +11,7 @@ var htmlPages = []; var navLevel = parentNavigationObject.length; - this.process = function () { + this.process = function (siteTitle, generator) { var childPages = getChildPages(); updateNavigationObject(childPages); if (isPage(title)) { @@ -20,7 +20,7 @@ content: generator.generate(node, siteTitle, title, navigationObject, navLevel) }); } - processChildren(childPages); + processChildren(childPages, siteTitle, generator); return htmlPages; }; @@ -29,18 +29,23 @@ $.each($(node).children(), function (index, child) { var childTitle = getTitle(child); if (isPage(childTitle)) { - if (childTitle[0] == '[') { - $.each(childTitle.substring(1, childTitle.length - 1).split(','), function(index, subTitle) { - pages.push({ title: subTitle, fileName: stripText(subTitle, true).toLowerCase(), node: child }); - }); - } else { - pages.push({ title: childTitle, fileName: stripText(childTitle, true).toLowerCase(), node: child }); - } + getChildTitles(childTitle).map(function(subTitle) { + pages.push({ title: subTitle, fileName: stripText(subTitle, true).toLowerCase(), node: child }); + }); } }); return pages; }; + function getChildTitles(childTitle) { + if (childTitle[0] == '[') { + return childTitle.substring(1, childTitle.length - 1).split(',') + } else { + return [childTitle]; + } + } + + function updateNavigationObject(childPages) { if (childPages.length > 0) { navigationObject.push(childPages.map(function (childPage) { @@ -49,11 +54,11 @@ } }; - function processChildren(childPages) { + function processChildren(childPages, siteTitle, generator) { $.each(childPages, function (index, childPage) { var path = fileName ? filePath + '/' + fileName : filePath; - var outline = new Outline(childPage.node, siteTitle, generator, childPage.title, childPage.fileName, path, navigationObject); - htmlPages = htmlPages.concat(outline.process()); + var outline = new Outline(childPage.node, childPage.title, childPage.fileName, path, navigationObject); + htmlPages = htmlPages.concat(outline.process(siteTitle, generator)); }); }; -}; \ No newline at end of file +}; diff --git a/test/outline.test.js b/test/outline.test.js index 41b1a00..738e6b6 100644 --- a/test/outline.test.js +++ b/test/outline.test.js @@ -19,18 +19,12 @@ describe('outline', function() { '' + '' + ''; - var generator = { - generate: function(node, siteTitle, title, navigationObject, navLevel) { - return arguments; - } - }; + outline = new Outline( $($.parseXML(opml)).find('body'), - 'Site Title', - generator, - 'Page Title', - 'pageTitle', - '/path/to', + 'Page Title', + 'pageTitle', + '/path/to', [[ { displayText: 'Home', @@ -46,9 +40,14 @@ describe('outline', function() { }); describe('process', function() { + var generator = { + generate: function(node, siteTitle, title, navigationObject, navLevel) { + return arguments; + } + }; var result; beforeEach(function() { - result = outline.process(); + result = outline.process('Site Title', generator); }); it('returns a list of html pages with structured file paths', function() { @@ -65,18 +64,18 @@ describe('outline', function() { expect(titleForPage(2)).toEqual('Footer - tag'); expect(titleForPage(3)).toEqual('Print'); }); - + it('calls the generator with the navigation object for the page', function() { expect(navObjectForPage(0).length).toEqual(2); expect(navObjectForPage(3)[2][0].path).toEqual('../footertag/print.html'); }); - + it('sets the selected state of parent pages in the navigation object', function() { expect(navObjectForPage(3)[0][0].selected).toEqual(true); expect(navObjectForPage(3)[1][1].selected).toEqual(true); expect(navObjectForPage(3)[2][0].selected).toEqual(true); }); - + it('calls the generator with the navigation level of the page', function() { expect(navLevelForPage(0)).toEqual(1); expect(navLevelForPage(1)).toEqual(2);