Add option to lazyload images
This commit is contained in:
parent
a355060d91
commit
4860306bb6
13
helper.py
13
helper.py
@ -1,6 +1,7 @@
|
||||
import os
|
||||
import stat
|
||||
import yaml
|
||||
import re
|
||||
|
||||
# Calls a YAML parser for the given relative path inside the presentation root
|
||||
# directory. Returns a dictionary with the YAML content.
|
||||
@ -29,4 +30,14 @@ def insert_metadata(content, root, lang):
|
||||
|
||||
# Retrieve the modification time for a given file.
|
||||
def get_modification_time(path):
|
||||
return os.stat(path)[stat.ST_MTIME]
|
||||
return os.stat(path)[stat.ST_MTIME]
|
||||
|
||||
# Replace the "src" attribute on images by "data-src".
|
||||
# Should be done after caching because fragments#defragmentize requires
|
||||
# valid XHTML and data-src causes problems with that.
|
||||
# Also: Toggling lazyload is easier this way.
|
||||
def add_lazyload(source, lazyload_images):
|
||||
if lazyload_images:
|
||||
return re.sub(r'<img(.*)src=', r'<img\1data-src=', source)
|
||||
else:
|
||||
return source
|
||||
|
||||
22
main.py
22
main.py
@ -10,28 +10,38 @@ import helper
|
||||
|
||||
# Compiles a presentation in the given language from the given directory and
|
||||
# stores it in a corresponding slides.lang.html file inside the same directory.
|
||||
def compile(root, language='en', force_recompile=False):
|
||||
|
||||
|
||||
def compile(root, language='en', force_recompile=False, lazyload_images=False):
|
||||
wrapper = open(os.path.join(root, 'layouts/root.html'), 'r').read()
|
||||
compiled_chapters = chapters.compile_chapters(root, language, force_recompile)
|
||||
wrapper = wrapper.replace('@slides', compiled_chapters)
|
||||
wrapper = helper.insert_metadata(wrapper, root, language)
|
||||
wrapper = fragments.defragmentize(wrapper)
|
||||
wrapper = helper.add_lazyload(wrapper, lazyload_images)
|
||||
with open(os.path.join(root, 'slides.' + language + '.html'), 'w+') as output:
|
||||
output.write(wrapper)
|
||||
print('done')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("rootdirectory", help="your project's root directory")
|
||||
parser.add_argument("-l", "--language", default="all", help="the presentation language (default: all)")
|
||||
parser.add_argument("-f", "--force-recompile", action='store_true', help="recompiling the entire presentation without caches")
|
||||
parser.add_argument("-l", "--language", default="all",
|
||||
help="the presentation language (default: all)")
|
||||
parser.add_argument("-f", "--force-recompile", action='store_true',
|
||||
help="recompile the entire presentation without caches")
|
||||
parser.add_argument("-i", "--lazyload-images", action='store_true',
|
||||
help="replace all images' src attributes by data-src for image lazyloading")
|
||||
|
||||
args = parser.parse_args()
|
||||
force_recompile = False or args.force_recompile
|
||||
lazyload_images = False or args.lazyload_images
|
||||
|
||||
if args.language == "all":
|
||||
for language in helper.get_available_languages(args.rootdirectory):
|
||||
compile(args.rootdirectory, language=language, force_recompile=force_recompile)
|
||||
compile(args.rootdirectory, language=language,
|
||||
force_recompile=force_recompile, lazyload_images=lazyload_images)
|
||||
else:
|
||||
compile(args.rootdirectory, language=args.language, force_recompile=force_recompile)
|
||||
|
||||
compile(args.rootdirectory, language=args.language,
|
||||
force_recompile=force_recompile, lazyload_images=lazyload_images)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user