Commit e5e277e2 authored by Rafael Monnerat's avatar Rafael Monnerat

Unify Javascript for Book/Chapter Render

parent 06f75d6e
No related merge requests found
...@@ -45,8 +45,7 @@ ...@@ -45,8 +45,7 @@
<link rel="stylesheet" type="text/css" tal:attributes="href string:${portal_url}/boom.css">\n <link rel="stylesheet" type="text/css" tal:attributes="href string:${portal_url}/boom.css">\n
<script type="text/javascript" tal:attributes="src string:${portal_url}/jquery/core/jquery.js"></script>\n <script type="text/javascript" tal:attributes="src string:${portal_url}/jquery/core/jquery.js"></script>\n
<script type="text/javascript" tal:attributes="src string:${portal_url}/jquery/ui/js/jquery-ui.js"></script>\n <script type="text/javascript" tal:attributes="src string:${portal_url}/jquery/ui/js/jquery-ui.js"></script>\n
<script type="text/javascript" tal:attributes="src string:${portal_url}/book_converter.js"></script>\n <script type="text/javascript" tal:attributes="src string:${portal_url}/html5_book.js"></script>\n
<script type="text/javascript" tal:attributes="src string:${portal_url}/chapter_converter.js"></script>\n
</tal:block>\n </tal:block>\n
<script type="text/javascript">\n <script type="text/javascript">\n
$(document).ready(function(){\n $(document).ready(function(){\n
......
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
<link rel="stylesheet" type="text/css" tal:attributes="href string:${portal_url}/boom.css">\n <link rel="stylesheet" type="text/css" tal:attributes="href string:${portal_url}/boom.css">\n
<script type="text/javascript" tal:attributes="src string:${portal_url}/jquery/core/jquery.js"></script>\n <script type="text/javascript" tal:attributes="src string:${portal_url}/jquery/core/jquery.js"></script>\n
<script type="text/javascript" tal:attributes="src string:${portal_url}/jquery/ui/js/jquery-ui.js"></script>\n <script type="text/javascript" tal:attributes="src string:${portal_url}/jquery/ui/js/jquery-ui.js"></script>\n
<script type="text/javascript" tal:attributes="src string:${portal_url}/chapter_converter.js"></script>\n <script type="text/javascript" tal:attributes="src string:${portal_url}/html5_book.js"></script>\n
</tal:block>\n </tal:block>\n
<script type="text/javascript">\n <script type="text/javascript">\n
$(document).ready(function(){\n $(document).ready(function(){\n
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
</item> </item>
<item> <item>
<key> <string>__name__</string> </key> <key> <string>__name__</string> </key>
<value> <string>book_converter.js</string> </value> <value> <string>html5_book.js</string> </value>
</item> </item>
<item> <item>
<key> <string>_vars</string> </key> <key> <string>_vars</string> </key>
...@@ -47,8 +47,238 @@ You should have received a copy of the GNU General Public License\n ...@@ -47,8 +47,238 @@ You should have received a copy of the GNU General Public License\n
along with this program; if not, write to the Free Software\n along with this program; if not, write to the Free Software\n
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n
*/\n */\n
var textContent, testPageHTML, body;\n
\n
function cleanForPrince(){\n
var temp = $(\'iframe\', parent.document).contents().children().clone();\n
$(\'script\', temp).remove();\n
$(\'style, meta:not([name=author])\', temp).remove();\n
$(\'head\', temp).prepend($(\'<meta>\').attr(\'http-equiv\',\'content-type\').attr(\'content\', \'text/html; charset=utf-8\'));\n
var images = $(\'img\', temp);\n
n = images.length;\n
for(var i = 0; i < n; i++){\n
var img = images.eq(i);\n
var src = img.attr(\'src\').split(\'?format\')[0].split(\'/\');\n
var extension = img.attr(\'type\');\n
if(extension == undefined)\n
extension = "png";\n
else\n
extension = extension.split(\'/\')[1].split(\'+\')[0];\n
img.attr(\'src\',src[src.length - 1] + \'.\' + extension);\n
}\n
var text = temp.html();\n
var result = "", tagName = "", c = "", chr = "";\n
var n = text.length;\n
if (text == null) {\n
return false;\n
}\n
var tag = false, tagNameParsing = false;\n
for(var i = 0; i < n; i++){\n
chr = text[i];\n
c = chr.toLowerCase();\n
if(c == \'<\' && tag == false){\n
tag = true;\n
tagNameParsing = true;\n
}\n
else if(tag){\n
if(c == \' \')\n
tagNameParsing = false;\n
else if(c == \'>\'){\n
tagNameParsing = false;\n
tag = false;\n
if(tagName == \'img\')\n
result += \'/\';\n
tagName = \'\';\n
}\n
else if(tagNameParsing){\n
if(c == \'i\' && tagName == \'\')\n
tagName += c;\n
else if(c == \'m\' && tagName == \'i\')\n
tagName += c;\n
else if(c == \'g\' && tagName == \'im\')\n
tagName += c;\n
}\n
}\n
result += chr;\n
}\n
$(\'textarea[name=field_book_text_content]\', parent.document).val(\'<html>\\n\' + result + \'\\n</html>\');\n
return false;\n
}\n
\n
function changeTag(element, tag){\n
var tag = $(\'<\' + tag + \'>\').html(element.html());\n
element.after(tag);\n
element.remove();\n
return tag;\n
}\n
\n
function parseList(text){\n
return text.replace("[","").replace("]","").replace(/\'/g,"").replace(/,/g,", ");\n
}\n
\n
function generateTblContent(title, id, className){\n
return $(\'<li>\').addClass(className).append($(\'<a>\').attr(\'href\',\'#\' + id).text(title));\n
}\n
\n
function generateToCLine(){\n
return generateTblContent("Table of Contents", \'toc-h-1\', \'frontmatter\');\n
}\n
\n
function generateFMLine(title, id){\n
return generateTblContent(title, \'frontmatter-h-\' + id, \'frontmatter\');\n
}\n
\n
function generateEMLine(title, id){\n
return generateTblContent(title, \'endmatter-h-\' + id, \'endmatter\');\n
}\n
\n
function generatePartLine(title, id){\n
return generateTblContent(title,\'part-h-\' + id, \'part\');\n
}\n
\n
function addLinksToChapters(chapterContainer, id){\n
var chapters = chapterContainer.children();\n
var n = chapters.length;\n
for(var i = 0; i < n; i++){\n
var chap = chapters.eq(i);\n
chap.addClass(\'chapter\');\n
$(\'a\', chap).attr(\'href\', \'#chapter-h-\' + id + \'-\' + (i+1));\n
}\n
return chapterContainer;\n
}\n
\n
function addToC(txt){\n
// Add to table of Contents\n
var ul = $(\'<ul>\').addClass(\'toc\');\n
var beginning = true;\n
var counter = 1, partCounter = 1;\n
$(\'body\').prepend($(\'<div>\').addClass(\'toc\').attr(\'id\',\'toc-h-1\').append(ul));\n
ul.html(txt);\n
ul.append(generateToCLine());\n
var headers = $(\'h1\', ul);\n
var n = headers.length;\n
for(var i = 0; i < n; i++){\n
var hdr = headers.eq(i);\n
var j = hdr.index() + 1;\n
var chapterList = ul.children().eq(j)[0];\n
if(chapterList == undefined || chapterList.tagName.toUpperCase() == "UL"){\n
beginning = false;\n
ul.append(generatePartLine(hdr.text(), partCounter).append(addLinksToChapters($(chapterList), partCounter)));\n
partCounter++;\n
counter = 1;\n
}\n
else{\n
if(beginning)\n
ul.append(generateFMLine(hdr.text(), counter));\n
else\n
ul.append(generateEMLine(hdr.text(), counter));\n
counter++;\n
}\n
}\n
$(\'> h1\', ul).remove();\n
}\n
\n
function fetchTextInfo(hasToC, txt){\n
$.get(\'TestPage_getDetail\', function(details, status, xhr){\n
details = details.split(\'\\n\');\n
var title = details[0], shortTitle = details[1], description = details[2], authors = parseList(details[3]);\n
var year = details[4].split(\'/\')[0];\n
var titleObj = $(\'title\');\n
if(titleObj.length > 0)\n
titleObj.text(title);\n
else\n
$(\'head\').append($(\'<title>\').text(title));\n
$(\'head\').prepend($(\'<meta>\').attr(\'name\',\'author\').attr(\'content\', authors));\n
titleObj = $(\'<h1>\').text(title);\n
var subtitle = $(\'<h2>\').text(description);\n
var edition = $(\'<h3>\').text(shortTitle);\n
\n
//Add Table of Contents\n
if(hasToC)\n
addToC(txt);\n
//Add imprint\n
$(\'body\').prepend($(\'<div>\').addClass(\'imprint\').append($(\'<p>\').text("Copyright \\u00A9 " + year + \' \' + authors)));\n
//Add Title Page\n
$(\'body\').prepend($(\'<div>\').addClass(\'titlepage\').append(titleObj.clone().addClass(\'no-toc\')).append(subtitle.clone().addClass(\'no-toc\')).append(edition.clone().addClass(\'no-toc\')).append($(\'<p>\').addClass(\'no-toc\').text(authors)));\n
//Add Front Cover\n
$(\'body\').prepend($(\'<div>\').addClass(\'frontcover\').append($(\'<img>\').attr(\'src\',\'canvas?format=\')).append(titleObj).append(subtitle).append(edition));\n
if(hasToC)\n
cleanForPrince();\n
});\n
}\n
\n
function convertChapter(link, container, first, isChapter, chapterCounter, partCounter){\n
$(function() {\n
//Getting the html content\n
$.get(link, function(data, textStatus, jqXHR){\n
var chapterContainer = $(\'<div>\').addClass(\'chapter\').attr(\'id\', \'chapter-h-\' + partCounter + \'-\' + chapterCounter).html(data);\n
link = link.replace("index_html?format=html","");\n
$(\'test\', chapterContainer).remove();\n
var sections = $(\'section\', chapterContainer);\n
$(\'base,meta,link,title\', chapterContainer).remove();\n
changeTag($(\'footer\', chapterContainer), \'p\');\n
var n = sections.length;\n
for(var i = 0; i < n; i++){\n
element = changeTag(sections.eq(i), \'div\');\n
var images = $(\'> img, details > img\', element);\n
var otherImages = $(\'img:not(> img, details > img)\', element);\n
var p = images.length;\n
for(var j = 0; j < p; j++){\n
var img = images[j];\n
var div = $(\'<div>\');\n
var caption = $(\'<p>\').addClass(\'caption\');\n
if((/^http/).test($(img).attr(\'src\')) == false) {\n
$(img).attr(\'src\', link + $(img).attr(\'src\'));\n
}\n
$(img).before(div);\n
var imgToAppend = $(img);\n
var imgWidth = $(img).attr(\'width\');\n
if( imgWidth == undefined || imgWidth == \'\')\n
imgToAppend.attr(\'width\',\'90%\');\n
div.addClass(\'figure\').append(caption).append($(\'<p>\').addClass(\'art\').append(imgToAppend));\n
caption.text($(img).attr(\'title\'));\n
}\n
var p = otherImages.length;\n
for(var j = 0; j < p; j++){\n
var img = otherImages[j];\n
if((/^http/).test($(img).attr(\'src\')) == false) {\n
$(img).attr(\'src\', link + $(img).attr(\'src\'));\n
}\n
}\n
if(first && i == 0)\n
element.attr(\'style\',\'counter-reset: page 1;\');\n
else if(i != 0){\n
var headers = $(\':header\', element);\n
var j = 0, p = headers.length;\n
for(j = 0; j < p; j++){\n
var hdr = headers[j];\n
changeTag($(hdr), \'H\' + (parseInt(hdr.tagName.split(\'H\')[1]) + 1));\n
}\n
}\n
var details = $(\'details\', element);\n
details.before(details.html());\n
details.remove();\n
element.addClass(\'section\');\n
}\n
//Why using this instead of load? because using load causes errors when the images are loaded, and the process won\'t reach the end in certain cases\n
//Moreover, since we load also descriptions, it\'s better to do it this way so that the user doesn\'t see the trick\n
chapterContainer.append($(\'div.section\', chapterContainer));\n
\n
$(\'test\', chapterContainer).remove();\n
//If it\'s a chapter and not the introduction or an appendix for instance\n
if(isChapter){\n
container.append(chapterContainer);\n
cleanForPrince();\n
}\n
else\n
container.append(chapterContainer.children());\n
if(container[0].tagName.toUpperCase() == \'BODY\'){\n
fetchTextInfo(false, \'\');\n
}\n
});\n
});\n
}\n
\n \n
var body;\n
function convertBook(linkToBook, container){\n function convertBook(linkToBook, container){\n
$(function() {\n $(function() {\n
//Getting the html content\n //Getting the html content\n
...@@ -104,7 +334,8 @@ function convertBook(linkToBook, container){\n ...@@ -104,7 +334,8 @@ function convertBook(linkToBook, container){\n
fetchTextInfo(true, data);\n fetchTextInfo(true, data);\n
});\n });\n
});\n });\n
} }\n
]]></string> </value> ]]></string> </value>
</item> </item>
......
33 34
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment