Advertisement
ashground

Scrivener to Textile

Oct 1st, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. """
  2.  
  3.       scrivener_to_textile.py
  4.       by aaron@andcuriouser.com
  5.    
  6.       Converts HTML exported from Scrivener to friendly Textile markup
  7.    
  8.       Prerequisities:
  9.               None
  10.    
  11.       Usage:
  12.               python scrivener_to_textile.py input.html
  13.  
  14. """
  15.  
  16.  
  17. import sys
  18. import xml.etree.ElementTree as ET
  19.  
  20. print('\nScrivener HTML to Textile\nby Aaron Scott Hildebrandt (andcuriouser.com)\n')
  21. print('Converts HTML exported from Scrivener to friendly Textile markup\n')
  22.  
  23. if len(sys.argv)<2:
  24.     print('--> You need to specify an HTML file')
  25.     sys.exit()
  26.  
  27. try: tree = ET.parse(sys.argv[1])
  28. except:
  29.     print('--> Can\'t parse file '+sys.argv[1])
  30.     sys.exit()
  31. root = tree.getroot()
  32. status = None
  33.  
  34. output = ""
  35.  
  36. count = 0
  37.  
  38. for paragraph in root.find('body').findall('p'):
  39.     paragraph = ET.tostringlist(paragraph)
  40.     for chunk in paragraph:
  41.         if chunk and chunk != '<span' and chunk != '<p' and chunk != '>':
  42.             if chunk.find('italic') != -1:
  43.                 status = 'italic'
  44.                 output += '_'
  45.             elif chunk.find('bold') != -1:
  46.                 status = 'bold'
  47.                 output += "*"
  48.             elif chunk == '</span>':
  49.                 if status == 'italic':
  50.                     output += '_'
  51.                 elif status == 'bold':
  52.                     output += '*'
  53.                 status = None
  54.                 if output[-2:] == ' _':
  55.                     output = output[0:-2]
  56.                     output += '_ '
  57.             elif chunk.find('style="') == -1:
  58.                 output += chunk
  59.  
  60. 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')
  61.  
  62. output_filename = ('.').join(sys.argv[1].split('.')[:-1])+'.textile'
  63.  
  64. try:
  65.     outgoing = open(output_filename, 'w')
  66.     outgoing.write(output)
  67.     outgoing.close()
  68.     print('--> Saved Textile markup as '+output_filename)
  69. except:
  70.     print('--> I ran into an error saving '+output_filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement