Last active
August 29, 2015 14:10
-
-
Save ashildebrandt/604dd1538fd3b3c85cbb to your computer and use it in GitHub Desktop.
Scrivener to Textile (converts Scrivener HTML exports to Textile)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
ash_scrivener_to_textile.py | |
by [email protected] | |
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('“', '"').replace('’', '\'').replace('”', '"').replace('—', '--').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