Posts

Showing posts from January, 2015

[Django CMS] Adding plugins inside plugins programatically

Image
I have a task to migrate a website. Old one is plain HTML and new one is Django CMS. I have a script that parses an HTML page from old website in to CMS page and places it into proper place.
Task is to migrate all the page content (that is a CMS TextPlugin) into an interlinked pages setup. Like we have text

<p>blah blah </p><a href="/page/url">Text</a><p> some other text</p>

And I need to change it into CMS LinkPlugin that is nested inside of the TextPlugin. It have become a common standard in the Django CMS world now. Note we need to have a LinkPlugin because of the requirement to interlink pages.
E.g. <a href="/page/url"> is a link to CMS Page object.

The solution is divided into two major parts. First we need to have the link plugin added to a placeholder of the text plugin. It must also be nested by the TextPlugin, like Django CMS admin web UI would do.  So our plugin would look somehow like this in the CMS Page admin:

Don't put html, head and body tags automatically into beautifulsoup content

Image
Was making a parser recently with BeautifulSoup. Came to the final with rendering contents of the edited text. Like so:
text="<h1>Test text tag</h1>"soup=BeautifulSoup(text,"html5")text=soup.renderContents()printtext It renders those contents with a result wrapped into the <html>, <head> and <body> tags. So print output looks like so:
'<html><head></head><body><h1>Test text tag</h1></body></html>' That's a feature of the html5lib library, it fixes HTML that is lacking, such as adding back in missing required elements.

The workaround is simple enough:
text=soup.body.renderContents() This solution will return an inside of the <html> tag <body>.
Result is:
text='<h1>Test text tag</h1>'