Skip to content

Commit

Permalink
Show progress bar for the ReST conversion process
Browse files Browse the repository at this point in the history
Previously the progress bar was shown for the total conversion of individual
input files which will be (currently) only one and hence it was not
looking good as did not show where it's actually taking the time.
  • Loading branch information
AswinChand97 authored and nifey committed Nov 20, 2021
1 parent 3445a16 commit 3590c6a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 43 deletions.
11 changes: 2 additions & 9 deletions cli.py
Expand Up @@ -5,10 +5,6 @@
import os
import shutil

def get_file_name(individual_file):
if individual_file is not None:
return individual_file.name

@click.command()
@click.option('-o','--output-directory',type=click.Path(),help=constants.cli_option_output_directory_help)
@click.option('-t','--theme','sphinx_theme_name',default="alabaster",type=str,prompt=True,help=constants.cli_option_theme_help)
Expand All @@ -34,11 +30,8 @@ def convert(output_directory,sphinx_theme_name,input_file,post_conversion,includ
click.echo("Aborting")
exit(1)

input_files= [input_file]
with click.progressbar(input_files,label="converting to sphinx",item_show_func=get_file_name) as bar:
for individual_file in bar:
c = epub2sphinx.Converter(individual_file.name,output_directory,sphinx_theme_name.lower(),include_custom_css)
c.convert()
c = epub2sphinx.Converter(input_file.name,output_directory,sphinx_theme_name.lower(),include_custom_css)
c.convert()

if post_conversion:
# Build using Sphinx
Expand Down
72 changes: 38 additions & 34 deletions epub2sphinx/convert.py
Expand Up @@ -14,6 +14,10 @@ def escape_quotes(text):
if text is not None:
return text.replace("'","\\'").replace('"','\\"')

def get_chapter_name(chapter):
if chapter is not None:
return chapter[0]

class Converter:

def __init__(self, file_name, output_directory, sphinx_theme_name,include_custom_css):
Expand Down Expand Up @@ -52,7 +56,6 @@ def convert(self):
os.path.join(self.output_directory, 'make.bat'))
self.get_chapter_names()
# Generate ReST file for each chapter in ebook
click.echo("Generating ReST files")
self.generate_rst()
# Generate index.rst
click.echo("Generating index.rst")
Expand Down Expand Up @@ -108,39 +111,40 @@ def generate_rst(self):
# Generate ReST file for each chapter in ebook
href_pattern = re.compile(r"(href=[\"\'][\w/.@-]*html)([#\'\"])")
svg_pattern = re.compile(r"\<svg[^\>]*\>(.*)\</svg\>", re.MULTILINE|re.DOTALL)
for chapter in self.epub.spine:
chapter_item = self.epub.get_item_with_id(chapter[0])
file_name = chapter_item.get_name()
if file_name in self.chapter_names.keys():
chapter_title = self.chapter_names[file_name]
else:
chapter_title = "Front page"

# Add filename to toctree
self.toctree.append(file_name)

# Create any parent directories as given in the filename
os.makedirs(os.path.dirname(os.path.join(self.source_directory, file_name)), exist_ok=True)

# Convert HTML to ReST
html_content = chapter_item.get_content().decode()
html_content = re.sub(href_pattern, r"\1.html\2", html_content)
if html_content.find("epub:type") != -1:
self.toctree.remove(file_name)
continue
if html_content.find("<svg") != -1:
html_content = re.sub(svg_pattern, r"\1", html_content)
html_content = html_content.replace("<image", "<img").replace("xlink:href", "src")
rst_content = pypandoc.convert_text(html_content, 'rst', format='html')

with open(os.path.join(self.source_directory, file_name + '.rst'), 'x') as ch_file:
# Add Chapter title
ch_file.write('*'*len(chapter_title)+'\n')
ch_file.write(chapter_title+'\n')
ch_file.write('*'*len(chapter_title)+'\n')

# Write ReST content
ch_file.write(rst_content)
with click.progressbar(self.epub.spine,show_eta=True,label="Generating ReST files",item_show_func=get_chapter_name) as bar:
for chapter in bar:
chapter_item = self.epub.get_item_with_id(chapter[0])
file_name = chapter_item.get_name()
if file_name in self.chapter_names.keys():
chapter_title = self.chapter_names[file_name]
else:
chapter_title = "Front page"

# Add filename to toctree
self.toctree.append(file_name)

# Create any parent directories as given in the filename
os.makedirs(os.path.dirname(os.path.join(self.source_directory, file_name)), exist_ok=True)

# Convert HTML to ReST
html_content = chapter_item.get_content().decode()
html_content = re.sub(href_pattern, r"\1.html\2", html_content)
if html_content.find("epub:type") != -1:
self.toctree.remove(file_name)
continue
if html_content.find("<svg") != -1:
html_content = re.sub(svg_pattern, r"\1", html_content)
html_content = html_content.replace("<image", "<img").replace("xlink:href", "src")
rst_content = pypandoc.convert_text(html_content, 'rst', format='html')

with open(os.path.join(self.source_directory, file_name + '.rst'), 'x') as ch_file:
# Add Chapter title
ch_file.write('*'*len(chapter_title)+'\n')
ch_file.write(chapter_title+'\n')
ch_file.write('*'*len(chapter_title)+'\n')

# Write ReST content
ch_file.write(rst_content)

def generate_index(self):
# Generate index.rst
Expand Down

0 comments on commit 3590c6a

Please sign in to comment.