Skip to content

Instantly share code, notes, and snippets.

@Kwpolska
Last active December 27, 2015 04:39
Show Gist options
  • Save Kwpolska/7268751 to your computer and use it in GitHub Desktop.
Save Kwpolska/7268751 to your computer and use it in GitHub Desktop.
mdx_nikola fix
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"document = \"\"\"\n",
"<h1>hello</h1>\n",
"<h1>hi</h1>\n",
"<h2>mornin'</h2>\n",
"\"\"\""
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import lxml.html\n",
"lxmldoc = lxml.html.fromstring(document)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"h1s = lxmldoc.xpath('//h1')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"h1 = h1s[0]\n",
"h1.tag = 'h2'\n",
"h1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 21,
"text": [
"<Element h2 at 0x7fb19411d770>"
]
}
],
"prompt_number": 21
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"lxml.html.tostring(lxmldoc)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 20,
"text": [
"b\"<div><h2>hello</h2>\\n<h1>hi</h1>\\n<h2>mornin'</h2>\\n</div>\""
]
}
],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"h1.tag = 'h1' #back to the original version"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def change_headers(lxmldoc):\n",
" for i in reversed(range(1, 6)): #html headers go to 6, so we can\u2019t \u201clower\u201d beneath five\n",
" elements = lxmldoc.xpath('//h' + str(i))\n",
" for e in elements:\n",
" e.tag = 'h' + str(i + 1)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"change_headers(lxmldoc)\n",
"lxml.html.tostring(lxmldoc)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 24,
"text": [
"b\"<div><h2>hello</h2>\\n<h2>hi</h2>\\n<h3>mornin'</h3>\\n</div>\""
]
}
],
"prompt_number": 24
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment