Zanteri - Programs

Programmer, Writer, Whatever Else

Story Conversion

      

import os
import re

stories = os.listdir('original')


for file_name in stories: 

    html_lines = []
    txt_lines = []

    with open('original/' + file_name, 'r', encoding='utf-8') as f:

        #Split txt file into paragraphs
        paragraphs = f.read().splitlines()

        #Remove whitespace indents
        #lines = [line.lstrip() for line in paragraphs]
        lines = [re.sub(r'^[\ufeff\s]+', '', line) for line in paragraphs]

        #Remove empty lines
        while("" in lines):
            lines.remove("")

        #FurAffinity
        for k in lines:
            #Bold
            k = re.sub(r'\*\*(.*?)\*\*', r'[b]\1[/b]', k)
    
            #Italics
            k = re.sub(r'\*(.*?)\*', r'[i]\1[/i]', k)

            k.replace("[i] [/i]", '* *')

            #Add multi-space indent before each line
            txt_lines.append('        ' + k)

            #Add gap between each item in list
            txt_lines.append('\n\n')

            

        #HTML
        for i in lines:
            #Bold
            i = re.sub(r'\*\*(.*?)\*\*', r'\1', i)
            
    
            #Italics
            i = re.sub(r'\*(.*?)\*', r'\1', i)

            i = re.sub(' ', '* *', i)

            html_lines.append('

' + i + '

\n') f.close try: with open('fa/' + file_name, 'x', encoding='utf-8') as h: print('Created FA: ' + file_name) #Write to txt with open('fa/' + file_name, 'a', encoding='utf-8') as h: for n in txt_lines: h.write(n) with open('html/' + file_name, 'x', encoding='utf-8') as g: print('Created HTML: ' + file_name) #Write to html with open('html/' + file_name, 'a', encoding='utf-8') as g: for l in html_lines: g.write(l) print('Saved: ' + file_name) except FileExistsError: print('Exists: ' + file_name)
Directory with .py file has three subdirectories: "original" where the original txt file goes, "html" where the html formatted txt file is saved, "fa" where the furaffinity formatted txt file is saved

Made by Zanteri

Feel free to email me any comments and feedback at [email protected]