From b177a6931fd380ef791a8ae314d7f76be8e6764b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 31 Mar 2016 10:48:13 +0530 Subject: [PATCH] E-book viewer: Make the detection of note boundaries for popup footnotes a little more robust --- resources/compiled_coffeescript.zip | Bin 104974 -> 105783 bytes src/calibre/ebooks/oeb/display/extract.coffee | 64 +++++++++++------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/resources/compiled_coffeescript.zip b/resources/compiled_coffeescript.zip index bcac0cd9b71f47b737eadcc5aa12fbf8425735f6..cf97c94acf927e137e0ff096e10bc43cc3317b5d 100644 GIT binary patch delta 1632 zcmb7EZExFD6wb}cDCy|f+HPZO>1ifvo4O${-I}${q_k28;sct-gw%i{JHAf5xOT9e zsik9SIt_{t+oap}wv!M;2n{hLhKg3fKS2BdgfxklA289+e1LnMbo0_7M5LV9=brPN z=RD`+Ui)tE)(?B{PAlDioUgrd_mi1l?hBz1|MrDKA(~xBU1V0d0jg;i%6ZLjE2c7^ zcfENNyK>X(DMgq3r3`giwukct+wv5{!VUp-VINejlA+;pTCrs^)du_Bfnq*&6mwI7WhTqn}Noo-rxblvW1A((v5tdaR(Brubypd1|wW zM5fF#NvhqO-4`eKO#50FN`|R9*b@B*X`BhO^Kr0SH9a?0sk$Xm!zT7H_)#&<=Kbb5 zfJC;>_mS24k2?axqqk;IFDejou9vLT`Xasrzz}BX_BjOP^SLwatR3dgT_AApP){KU z$xb|{iX}Sm7F(!~?j6{SzPr39jtx!rF=Wu~s--m~cRj`NWVj$hSq2kZzDdYXQR&%3 zEdAK!JNJd&RzT#)lAjW2C(v+XCD^~!wM`r|?V>15I#?+iR*?;( zv94fSpjd^H?Tmo{L%Z2dHt4qS1!%6;ztIY=2H8#}$WQeb$%A@y6yU!JRg@wIl~V;3 z>$Zahwy+E%xzztA1n--M1i|&^J^!x)itb^jiEk|(CqqjeIG6jeuVt_ z#dB9uSGQ7EQG9>r<98~3X9eZj&V9IZujbzGq}wa#rM92Rj_cPO9W=AbT!+8farNJh z&}Ht*Z8cr=$5oVU`+Wd4?q_;%4JE_TG@V{UXTl$3>Gd`ATKMt|y}yQH;Zv0Eqv(0U zi~Y8NhQl(@0~B?$TRrk}bPS?uI*m_hC-rz@1jn&Dk{V4Zx~h)EPcVm)9*yftQc37q tw2^p+KC_OdXnX@bO|P9s&(iA~Xo_&&U#_EN+DB14ePbPEc|-=i_Yd-!AaDQx delta 901 zcmZuwT}YEr7~Z?-CNnK_{^Im3&22hTx4CI6Z=xRw!;7rQ+4#17pDw>Pn=1wNB7ZhV zD#|OJ+$PdZ7eQf6Fp8iby67Sax(Z6Yt_ZuUNa_2wSty@AuxD547yP+}hw#4@KI?amwVRVSMpKi_{rhFODId0iei z>u_~>c(Jouok3ZVf0y`OhJv1&eCqM&XdG?xtc)Sj;FTe^xuY>dX0^-Ku@_7S{ z$IJQ+xoH+AJMW>2DE@L?}PTYD}lgcN@3CHlgtW8N0V^1~V5dIk{ ziMxkq0dJ04u2aD4@Yz*LA;ESpitE?oNPB!4^VLDld)6X*9I`P@`?OKKluzu~XYsD^Uw(ZW? zRZ`)R1Uopn){I9DO8mT5pde&NX=%8*ZW*zUFWbkVcK@yqjz%c7ORIwD3zyU4yHF*4 zG%dt=R2_p%%89`D`w5^s21@!^1dK}}fD_9Z^tZ5=Jt+n339IP02xQO`bHa&VX6l=R z(`i?lKti8Ip^SbMU|HKmpwY{qE^IxUE|&;HDa return true return false +block_tags = ['p', 'div', 'li', 'td', 'h1', 'h2', 'h2', 'h3', 'h4', 'h5', 'h6', 'body'] +block_display_styles = ['block', 'list-item', 'table-cell', 'table'] + +get_note_container = (node) -> + until node.tagName.toLowerCase() in block_tags or is_epub_footnote(node) or getComputedStyle(node).display in block_display_styles + node = node.parentNode + if not node + break + return node + get_parents_and_self = (node) -> ans = [] while node and node isnt document.body @@ -124,9 +134,15 @@ hide_children = (node) -> if child.nodeType == Node.ELEMENT_NODE if child.do_not_hide hide_children(child) + delete child.do_not_hide else child.style.display = 'none' +unhide_tree = (elem) -> + elem.do_not_hide = true + for c in elem.getElementsByTagName('*') + c.do_not_hide = true + class CalibreExtract # This class is a namespace to expose functions via the # window.calibre_extract object. @@ -154,8 +170,7 @@ class CalibreExtract start_elem = document.getElementById(target) if not start_elem return - in_note = false - is_footnote_container = is_epub_footnote(start_elem) + start_elem = get_note_container(start_elem) for elem in get_parents_and_self(start_elem) elem.do_not_hide = true style = window.getComputedStyle(elem) @@ -163,31 +178,32 @@ class CalibreExtract # We cannot display list numbers since they will be # incorrect as we are removing siblings of this element. elem.style.listStyleType = 'none' - for elem in document.body.getElementsByTagName('*') - if in_note - if known_targets.hasOwnProperty(elem.getAttribute('id')) - in_note = false - continue - pb = get_page_break(elem) - if pb['before'] - in_note = false - else if pb['after'] - in_note = false + if is_epub_footnote(start_elem) + unhide_tree(start_elem) + else + # Try to detect natural boundaries based on markup for this note + found_note_start = false + for elem in document.body.getElementsByTagName('*') + if found_note_start + eid = elem.getAttribute('id') + if eid != target and known_targets.hasOwnProperty(eid) and get_note_container(elem) != start_elem + console.log('Breaking footnote on anchor: ' + elem.getAttribute('id')) + delete get_note_container(elem).do_not_hide + break + pb = get_page_break(elem) + if pb['before'] + console.log('Breaking footnote on page break before') + break + if pb['after'] + unhide_tree(elem) + console.log('Breaking footnote on page break after') + break elem.do_not_hide = true - for child in elem.getElementsByTagName('*') - child.do_not_hide = true - else - elem.do_not_hide = true - else - if elem is start_elem - in_note = not is_footnote_container and not get_page_break(elem)['after'] - if not in_note - for child in elem.getElementsByTagName('*') - child.do_not_hide = true + else if elem is start_elem + found_note_start = true + hide_children(document.body) location.hash = '#' + target if window? window.calibre_extract = new CalibreExtract() - -