Created
November 13, 2016 13:05
-
-
Save Lee-W/e18a095124bd90232c7f463deec1b072 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import argparse | |
from distutils.util import strtobool | |
from bs4 import BeautifulSoup | |
def externaljs_to_static(path_suffix, input_file_name, output_file_name): | |
index_path = os.path.join(path_suffix, input_file_name) | |
output_path = os.path.join(path_suffix, output_file_name) | |
print(index_path, output_path) | |
with open(index_path, 'r') as index_file: | |
index_content = index_file.read() | |
soup = BeautifulSoup(index_content, 'html5lib') | |
for section_soup in soup.find_all('section'): | |
if section_soup.has_attr('data-external'): | |
sub_path = os.path.join(path_suffix, section_soup['data-external']) | |
with open(sub_path, 'r') as sub_file: | |
sub_content = sub_file.read() | |
sub_soup = BeautifulSoup(sub_content, 'html.parser') | |
section_soup.attrs = {} | |
section_soup.insert(0, sub_soup) | |
if os.path.isfile(output_path): | |
print('{} exists.\n'.format(output_path)) | |
ans = input('Overwrite? (y/n) ') | |
if not strtobool(ans): | |
return | |
with open(output_path, 'w') as output_file: | |
output_file.write(str(soup)) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument('path', | |
help='Path of reveal.js project with external.js') | |
parser.add_argument('-i', '--input', | |
default='index.html', | |
help='Name of index file') | |
parser.add_argument('-o', '--output', | |
default='index.html', | |
help='Output File Name') | |
args = parser.parse_args() | |
externaljs_to_static(args.path, args.input, args.output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment