Merge branch 'imglink' of XPUB/special-issue-11-wiki2html into master

master
acastro 5 years ago committed by Gitea
commit 4a943f9a7f

@ -66,15 +66,26 @@ def filenameforlink(href):
href = urlquote(href) href = urlquote(href)
return href return href
def rewritelinks (html):
t = html5lib.parseFragment(html, treebuilder = "etree", namespaceHTMLElements = False)
# remove links to wiki File: pages def rewriteimglinks(tree, page):
for a in t.findall(".//a[@class='image']"): # select img wrapping a # invoke after img src has been rewritten
href = a.attrib.get('href') # To: remove links to wiki File on all pages
if a.findall(".//img") and 'File:' in href: # ensure a has child: img # but Overview_main_page page where link to publication page is added
a.attrib['href'] = 'javascript:void(0);' # disable href if page.name == 'Overview main page':
for div_parent in tree.findall(".//div[@class='tooltip']"):
anchor_of_img = div_parent.find(".//div/a")
if anchor_of_img.find(".//img") is not None: # <a> needs child <img>
a_tag = div_parent.find(".//p/span/a")
publication_href = a_tag.attrib.get('href')
anchor_of_img.attrib['href'] = publication_href
else:
for a in tree.findall(".//a[@class='image']"): # select img wrapping a
if a.findall(".//img"): # ensure a has child: img
a.attrib['href'] = 'javascript:void(0);' # disable href
return tree
def rewritelinks(html):
t = html5lib.parseFragment(html, treebuilder = "etree", namespaceHTMLElements = False)
for a in t.findall(".//*[@href]"): for a in t.findall(".//*[@href]"):
linkclass = a.attrib.get("class", "") linkclass = a.attrib.get("class", "")
href = a.attrib.get("href") href = a.attrib.get("href")
@ -89,7 +100,7 @@ def rewritelinks (html):
return html return html
def rewriteimgs(html): def rewriteimgs(html, page):
t = html5lib.parseFragment(html, treebuilder = "etree", namespaceHTMLElements = False) t = html5lib.parseFragment(html, treebuilder = "etree", namespaceHTMLElements = False)
# replace images url with local image in ../images # replace images url with local image in ../images
@ -119,6 +130,9 @@ def rewriteimgs(html):
img.attrib['srcset'] = "" # rm srcset value:it prevent imgs displaying img.attrib['srcset'] = "" # rm srcset value:it prevent imgs displaying
img.attrib['width'] = "" img.attrib['width'] = ""
img.attrib['height'] = "" img.attrib['height'] = ""
t = rewriteimglinks(tree=t, page=page)
html = ET.tostring(t, method="html", encoding="unicode") html = ET.tostring(t, method="html", encoding="unicode")
return html return html
@ -126,7 +140,7 @@ def dumppage(p, template, rewrite_images=True):
htmlsrc = site.parse(page=p.name)['text']['*'] htmlsrc = site.parse(page=p.name)['text']['*']
htmlsrc = rewritelinks(htmlsrc) htmlsrc = rewritelinks(htmlsrc)
if rewrite_images: if rewrite_images:
htmlsrc = rewriteimgs(htmlsrc) htmlsrc = rewriteimgs(html=htmlsrc, page=p)
html = template.render(page=p, body=htmlsrc, staticpath='.') html = template.render(page=p, body=htmlsrc, staticpath='.')
with open(os.path.join(args.output, filenameforpage(p)), 'w') as f: with open(os.path.join(args.output, filenameforpage(p)), 'w') as f:
f.write(html) f.write(html)

@ -293,3 +293,5 @@ margin-left: 40px;
height: 2px; height: 2px;
background-color: #0BEFEB; background-color: #0BEFEB;
} }
a.image {cursor: pointer!important;} /* KEEP THIS: show imgs as link in Overview */
Loading…
Cancel
Save