Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- scrivener_to_textile.py
- by aaron@andcuriouser.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('“', '"').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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement