Skip to content

Instantly share code, notes, and snippets.

@ashildebrandt
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashildebrandt/604dd1538fd3b3c85cbb to your computer and use it in GitHub Desktop.
Save ashildebrandt/604dd1538fd3b3c85cbb to your computer and use it in GitHub Desktop.
Scrivener to Textile (converts Scrivener HTML exports to Textile)
"""
ash_scrivener_to_textile.py
by me@aaronhildebrandt.com
Converts HTML exported from Scrivener to friendly Textile markup
Prerequisities:
None
Usage:
python scrivener_to_textile.py input.html
"""
import sys
import xml.etree.ElementTree as ET
print('\nScrivener HTML to Textile\nby Aaron Scott Hildebrandt (andcuriouser.com)\n')
print('Converts HTML exported from Scrivener to friendly Textile markup\n')
if len(sys.argv)<2:
print('--> You need to specify an HTML file')
sys.exit()
try: tree = ET.parse(sys.argv[1])
except:
print('--> Can\'t parse file '+sys.argv[1])
sys.exit()
root = tree.getroot()
status = None
output = ""
count = 0
for paragraph in root.find('body').findall('p'):
paragraph = ET.tostringlist(paragraph)
for chunk in paragraph:
if chunk and chunk != '<span' and chunk != '<p' and chunk != '>':
if chunk.find('italic') != -1:
status = 'italic'
output += '_'
elif chunk.find('bold') != -1:
status = 'bold'
output += "*"
elif chunk == '</span>':
if status == 'italic':
output += '_'
elif status == 'bold':
output += '*'
status = None
if output[-2:] == ' _':
output = output[0:-2]
output += '_ '
elif chunk.find('style="') == -1:
output += chunk
output = output.replace('<p>', '').replace('</p>', '\n').replace('&#8220;', '"').replace('&#8217;', '\'').replace('&#8221;', '"').replace('&#8212;', '--').replace('\n<br />\n\n--\n\n<br />\n', '\n<hr />\n')
output_filename = ('.').join(sys.argv[1].split('.')[:-1])+'.textile'
try:
outgoing = open(output_filename, 'w')
outgoing.write(output)
outgoing.close()
print('--> Saved Textile markup as '+output_filename)
except:
print('--> I ran into an error saving '+output_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment