Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding URL_TYPE via meta #2544

Merged
merged 2 commits into from Oct 23, 2016
Merged

Allow overriding URL_TYPE via meta #2544

merged 2 commits into from Oct 23, 2016

Conversation

@Kwpolska
Copy link
Member

@Kwpolska Kwpolska commented Oct 22, 2016

Useful for HTML fragments inserted via JavaScript that contain links.

Use case: nikola-plugins-v2 and its side menu that can be changed
without rebuilding all pages.

Kwpolska added 2 commits Oct 22, 2016
Useful for HTML fragments inserted via JavaScript that contain links.

Use case: nikola-plugins-v2 and its side menu that can be changed
without rebuilding all pages.
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
Copy link
Contributor

@felixfontein felixfontein left a comment

LGTM from a technical point of view.

@felixfontein
Copy link
Contributor

@felixfontein felixfontein commented Oct 23, 2016

I'm wondering how you're using this to create HTML fragments. To me it seems hard to get Nikola to not create whole HTML pages but only fragments.

Wouldn't it make more sense to write a plugin to create such HTML fragments?

@Kwpolska
Copy link
Member Author

@Kwpolska Kwpolska commented Oct 23, 2016

I’m creating fragments for some value of fragments. Our current rendering pipeline (lxml) will add a DOCTYPE, <html> and <body>. This is good enough, because jQuery’s $.load('/sidemenu.html div#sidemenu') will take care of this.

And if I really wanted fragments, I could make a micro-plugin to copy /cache/pages/sidemenu.html to /output/real_sidemenu.html as-is.

@Kwpolska Kwpolska merged commit 926bcc8 into master Oct 23, 2016
4 checks passed
@Kwpolska Kwpolska deleted the per_post_url_type branch Oct 23, 2016
@felixfontein
Copy link
Contributor

@felixfontein felixfontein commented Oct 23, 2016

Wouldn't it still be nice if it would be possible to instruct generic_renderer and render_template to allow outputting a real fragment? This would also allow to do a simple text substitution to do the work on the server, instead of using JavaScript on the client.

@Kwpolska
Copy link
Member Author

@Kwpolska Kwpolska commented Oct 23, 2016

That’s going to be slightly tricky, but we could skip the lxml part. This would have side effects (like the possibility of broken HTML in output or URL_TYPE effectively being completely ineffective, and I don’t want any new not server root bugs)

Unless we could tell lxml to output fragments, if that’s possible.

@felixfontein
Copy link
Contributor

@felixfontein felixfontein commented Oct 23, 2016

From here: http://stackoverflow.com/questions/3443831/python-and-elementtree-return-inner-xml-excluding-parent-element it looks it can be done like this:

parser = lxml.html.HTMLParser(remove_blank_text=True)
doc = lxml.html.fragment_fromstring(data, parser)

doc.rewrite_links(lambda url: rewrite(url))
for obj in list(doc.xpath('(//img|//source)')):
    if 'srcset' in obj.attrib:
        ...

data = (body.text or '').encode('utf-8') + ''.encode('utf-8').join([lxml.html.tostring(child, encoding='utf-8', method='html') for child in body.iterchildren()])

So essentially parsing the document is changed a little (fragment_fromstring instead of document_fromstring), the srcset fix is changed a little (removal of * in '(*//img|*//source)'), and recombining the doc into a HTML fragment changes (call tostring on children, joining the result and prepending doc.text). (If we'd just call tostring on the whole of doc, a <div></div> is added to stand-alone text.)

@Kwpolska
Copy link
Member Author

@Kwpolska Kwpolska commented Oct 23, 2016

That <div></div> sounds like a good compromise to me if that’s the only thing that happens.

@felixfontein
Copy link
Contributor

@felixfontein felixfontein commented Oct 23, 2016

That was the only thing I noticed in a very limited minimal test. So it could be that worse things happen :)

@felixfontein
Copy link
Contributor

@felixfontein felixfontein commented Oct 23, 2016

It actually seems to always add a <div></div> around the whole output.

@felixfontein
Copy link
Contributor

@felixfontein felixfontein commented Oct 23, 2016

(So <div>1</div> gets converted to <div><div>1</div></div> with a newline before the final </div>.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants