Skip to content

Commit

Permalink
Merge pull request #112 from aless80/issue100
Browse files Browse the repository at this point in the history
jupyterbook fix path of images and figures
  • Loading branch information
aless80 committed Mar 3, 2021
2 parents 35f1e0c + ee33a98 commit ea41df3
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 105 deletions.
2 changes: 1 addition & 1 deletion lib/doconce/DocWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ def figure(self, filename, caption, width=None, height=None, label=None):
height = ' width=%s ' % width
else:
height = ''
s = '\n<hr><img src="%s"%s%s>\n<p><em>%s</em>\n<hr><p>\n' % \
s = '\n<hr><img src="%s"%s%s>\n<p><em>%s</em></p>\n<hr><p>\n' % \
(filename, width, height, caption)
self.file.write(s)

Expand Down
93 changes: 16 additions & 77 deletions lib/doconce/doconce.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ def errwarn(msg, end='\n', style=''):
err.write('\n')

from .common import *
from .misc import option, which, _abort
from .misc import option, which, _abort, help_format, check_command_line_options, find_file_with_extensions
from . import html, latex, pdflatex, rst, sphinx, st, epytext, gwiki, mwiki, cwiki, pandoc, ipynb, matlabnb
from . import plaintext as plain
from .latex import aux_label2number
from .html import embed_IBPLOTs
from .expand_newcommands import expand_newcommands

main_content_begin = globals.main_content_char*19 + ' main content ' + \
globals.main_content_char*22
Expand Down Expand Up @@ -200,8 +203,7 @@ def markdown2doconce(filestr_in, format=None, ipynb_mode=False):
perl='pl', bash='sh', html='html')

bc_postfix = '-t' if ipynb_mode else ''
from .common import unindent_lines
regex = [
regex_md2doconce = [
# Computer code with language specification
(r"\n?```([A-Za-z]+)(.*?)\n```", lambda m: "\n\n!bc %scod%s%s\n!ec\n" % (extended_markdown_language2dolang[m.group(1).lower()], bc_postfix, unindent_lines(m.group(2).rstrip(), trailing_newline=False)), re.DOTALL), # language given
# Computer code without (or the same) language specification
Expand Down Expand Up @@ -257,7 +259,7 @@ def markdown2doconce(filestr_in, format=None, ipynb_mode=False):
# doconce-translated ipynb files, treat remaining div tags as labels
(r'<div id="(.+)?"></div>\n', r'label{\g<1>}\n'),
]
for r in regex:
for r in regex_md2doconce:
if len(r) == 2:
filestr = re.sub(r[0], r[1], filestr)
elif len(r) == 3:
Expand Down Expand Up @@ -1476,14 +1478,12 @@ def system(cmd):
return filestr, num_commands

def exercises(filestr, format, code_blocks, tex_blocks):
# Exercise:
""" Exercise:
# ===== Exercise: title ===== (starts with at least 3 =, max 5)
# label{some:label} file=thisfile.py solution=somefile.do.txt
# __Hint 1.__ some paragraph...,
# __Hint 2.__ ...

from .common import _CODE_BLOCK, _MATH_BLOCK

"""
all_exer = [] # collection of all exercises
exer = {} # data for one exercise, to be appended to all_exer
inside_exer = False
Expand Down Expand Up @@ -1833,7 +1833,6 @@ def exercises(filestr, format, code_blocks, tex_blocks):

# Append solutions and answer to filestr (in the end of the doconce string)
if option('solutions_at_end') or option('answers_at_end') or has_sol_docend or has_ans_docend:
from .common import chapter_pattern
has_chapters = False
if re.search(chapter_pattern, filestr, flags=re.MULTILINE):
has_chapters = True
Expand Down Expand Up @@ -3118,7 +3117,6 @@ def handle_cross_referencing(filestr, format, tex_blocks):

# 3. Replace ref by hardcoded numbers from a latex .aux file
refaux = 'refaux{' in filestr
from .latex import aux_label2number
label2number = aux_label2number()
if format not in ('latex', 'pdflatex') and refaux and not label2number:
errwarn('*** error: used refaux{} reference(s), but no option --replace_ref_by_latex_auxno=')
Expand Down Expand Up @@ -3567,7 +3565,6 @@ def typeset_authors(filestr, format):
def typeset_section_numbering(filestr, format):
chapter = section = subsection = subsubsection = 0
# Do we have chapters?
from .common import chapter_pattern
if re.search(chapter_pattern, filestr, flags=re.MULTILINE):
has_chapters = True
else:
Expand Down Expand Up @@ -3991,7 +3988,6 @@ def inline_tag_subst(filestr, format):

# Add copyright right under the date if present
if format not in ('html', 'latex', 'pdflatex', 'sphinx'):
from .common import get_copyfile_info
cr_text = get_copyfile_info(filestr, format=format)
if cr_text is not None:
if cr_text == 'Made with DocOnce':
Expand Down Expand Up @@ -4482,7 +4478,6 @@ def report_progress(msg):
m = re.search('^IBPLOT: *\[', filestr, flags=re.MULTILINE)
has_ibplot = True if m else False
if has_ibplot:
from .html import embed_IBPLOTs
filestr, bg_session = embed_IBPLOTs(filestr, format)
#bg_session.loop_until_closed()
debugpr('The file after inserting interactive IBPLOT curve plots:', filestr)
Expand Down Expand Up @@ -4523,7 +4518,6 @@ def print_blocks(blocks, delimiter=True):

# Next step: substitute latex-style newcommands in filestr and tex_blocks
# (not in code_blocks)
from .expand_newcommands import expand_newcommands
if format not in ('latex', 'pdflatex'):
newcommand_files = glob.glob('newcommands*_replace.tex')
if format in ('sphinx', 'pandoc', 'ipynb'):
Expand Down Expand Up @@ -5307,57 +5301,6 @@ def version_compare(a, b):
return resultfile



def parse_doconce_filename(filename_in):
"""Parse the DocOnce filename. Abort if the file is not found
:param str filename_in: Filename. The DocOnce extensions '.do.txt' can be omitted
:return: (dirname, basename, extension, filename) tuple
:rtype: (str, str, str, str)
"""
filename_out = filename_in
dirname, basename = os.path.split(filename_out)
if dirname:
os.chdir(dirname)
filename_out = basename
# errwarn('*** doconce format now works in directory %s' % dirname)
# cannot call errwarn before globals.dofile_basename is initialized
# print instead
print('*** doconce format now works in directory %s' % dirname)

basename, ext = os.path.splitext(basename)
# Can allow no extension, .do, or .do.txt
legal_extensions = ['.do', '.do.txt']
if ext == '':
found = False
for ext in legal_extensions:
filename_out = basename + ext
if os.path.isfile(filename_out):
found = True
break
if not found:
print('*** error: given doconce file "%s", but no' % basename)
print(' files with extensions %s exist' % ' or '.join(legal_extensions))
_abort()
else:
# Given extension
if not os.path.isfile(filename_out):
print('*** error: file %s does not exist' % filename_out)
_abort()
if ext == '.txt':
if filename_out.endswith('.do.txt'):
basename = filename_out[:-7]
else: # just .txt
basename = filename_out[:-4]
elif ext == '.do':
basename = filename_out[:-3]
else:
print('*** error: illegal file extension %s' % ext)
print(' must be %s' % ' or '.join(legal_extensions))
_abort()
return dirname, basename, ext, filename_out


def format_driver():
# doconce format accepts special command-line arguments:
# - debug (for debugging in file _doconce_debugging.log) or
Expand All @@ -5369,11 +5312,9 @@ def format_driver():
# oneline is inactive (doesn't work well yet)

if option('help') or '-h' in sys.argv:
from .misc import help_format
help_format()
sys.exit(1)

from .misc import check_command_line_options
check_command_line_options(4)

try:
Expand Down Expand Up @@ -5407,19 +5348,17 @@ def format_driver():
if option('debug'):
globals._log_filename = '_doconce_debugging.log'
globals._log = open(globals._log_filename,'w')
globals._log.write("""
This is a log file for the doconce script.
Debugging is turned on by the command-line argument '--debug'
to doconce format. Without that command-line argument,
this file is not produced.
""")
globals._log.write(('\n'
' This is a log file for the doconce script.\n'
' Debugging is turned on by the command-line argument \'--debug\'\n'
' to doconce format. Without that command-line argument,\n'
' this file is not produced.\n'
'\n'))
print('*** debug output in ' + globals._log_filename)


debugpr('\n\n******* output format: %s *******\n\n' % format)

dirname, basename, ext, globals.filename = parse_doconce_filename(globals.filename)
dirname, basename, ext, globals.filename = find_file_with_extensions(globals.filename,
allowed_extensions=['.do.txt'])
globals.dofile_basename = basename

_rmdolog() # always start with clean log file with errors
Expand Down
28 changes: 14 additions & 14 deletions lib/doconce/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
('blue', '#0000FF', 'rgb(0,0,255)'),
('navy', '#000080', 'rgb(0,0,128)'),]

# html code and corresponding regex (here for reusability)
movie2html = {
'.mp4': "\n <source src='%(stem)s.mp4' type='video/mp4; codecs=\"avc1.42E01E, mp4a.40.2\"'>",
'.webm': "\n <source src='%(stem)s.webm' type='video/webm; codecs=\"vp8, vorbis\"'>",
'.ogg': "\n <source src='%(stem)s.ogg' type='video/ogg; codecs=\"theora, vorbis\"'>",
'movie_regex':
r'<(\w+) src=\'(.+)\'\s+type=\'video/(?:mp4|webm|ogg);\s+codecs=[\\]{0,1}\".+[\\]{0,1}\"\'>',
}


def add_to_file_collection(filename, doconce_docname=None, mode='a'):
Expand Down Expand Up @@ -2020,14 +2028,6 @@ def html_movie(m):
"<div>\n"
"<video %(autoplay)s loop controls width='%(width)s' height='%(height)s' preload='none'>") \
% vars()
ext2source_command = {
'.mp4': """
<source src='%(stem)s.mp4' type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>""" % vars(),
'.webm': """
<source src='%(stem)s.webm' type='video/webm; codecs="vp8, vorbis"'>""" % vars(),
'.ogg': """
<source src='%(stem)s.ogg' type='video/ogg; codecs="theora, vorbis"'>""" % vars(),
}
movie_exists = False
mp4_exists = False
if sources3:
Expand All @@ -2037,19 +2037,19 @@ def html_movie(m):
# can play on iOS.
msg = 'movie: trying to find'
if is_file_or_url(stem + '.mp4', msg) in ('file', 'url'):
text += ext2source_command['.mp4']
text += movie2html['.mp4'] % vars()
movie_exists = True
mp4_exists = True
if is_file_or_url(stem + '.webm', msg) in ('file', 'url'):
text += ext2source_command['.webm']
text += movie2html['.webm'] % vars()
movie_exists = True
if is_file_or_url(stem + '.ogg', msg) in ('file', 'url'):
text += ext2source_command['.ogg']
text += movie2html['.ogg'] % vars()
movie_exists = True
else:
# Load just the specified file
if is_file_or_url(stem + ext, msg) in ('file', 'url'):
text += ext2source_command[ext]
text += movie2html[ext] % vars()
movie_exists = True
if not movie_exists:
errwarn('*** warning: movie "%s" was not found' % filename)
Expand All @@ -2062,8 +2062,8 @@ def html_movie(m):
text += ("\n"
"</video>\n"
"</div>\n"
"<p><em>%(caption)s</em></p>\n"
) % vars()
"<p><em>%s</em></p>\n"
) % caption
#if not mp4_exists:
if True:
# Seems that there is a problem with .mp4 movies as well...
Expand Down
31 changes: 25 additions & 6 deletions lib/doconce/ipynb.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@
figure_labels = {}
html_encountered = False

# html code and corresponding regex (here for reusability)
img2ipynb = {
'imgtag':
('\n<img src="{filename}" {opts}>'
'<p style="font-size: 0.9em"><i>Figure {figure_number}: {caption}</i></p>'
),
'imgtag_regex':
r'<(\w+) src=[\\]"(.+)[\\]" .*>(?=[<|\\n])',
'md':
'\![{caption}]({filename})',
'md_regex':
r'\!\[(?:Figure )(\d+)\]\((.+)\)',
'Image':
'Image(%{keyword}="{filename}")\n',
'Image_regex':
'Image\((\w+)=(\w+)\)',
}


def ipynb_author(authors_and_institutions, auth2index,
inst2index, index2inst, auth2email):
# Old code
Expand Down Expand Up @@ -119,17 +138,17 @@ def ipynb_figure(m):
if display_method == 'md':
# Markdown image syntax for embedded image in text
# (no control of size, then one must use HTML syntax)
text += '\![%s](%s)' % (caption, filename)
text += img2ipynb['md'].format(caption, filename)
elif display_method == 'imgtag':
# Plain <img tag, allows specifying the image size
# Fix caption markup so it becomes html
for tag in 'bold', 'emphasize', 'verbatim':
caption = re.sub(INLINE_TAGS[tag], INLINE_TAGS_SUBST['html'][tag],
caption, flags=re.MULTILINE)
text += ('\n'
'<img src="{filename}" {opts}>'
'<p style="font-size: 0.9em"><i>Figure {figure_number}: {caption}</i></p>'
).format(filename=filename, opts=opts, caption=caption, figure_number=figure_number)
text += img2ipynb['imgtag'].format(filename=filename,
opts=opts,
caption=caption,
figure_number=figure_number)
elif display_method == 'Image':
# Image object
# NOTE: This code will normally not work because it inserts a verbatim
Expand All @@ -150,7 +169,7 @@ def ipynb_figure(m):
keyword = 'url'
else:
keyword = 'filename'
text += 'Image(%s="%s")\n' % (keyword, filename)
text += img2ipynb['Image'].format(keyword, filename)
text += '!ec\n'
else:
errwarn('*** error: --ipynb_figure=%s is illegal, must be md, imgtag or Image' % display_method)
Expand Down

0 comments on commit ea41df3

Please sign in to comment.