From c1778dcd71e57f958ba36318046a2bdb55eed7e8 Mon Sep 17 00:00:00 2001 From: Abhay Rana Date: Sat, 18 Feb 2017 12:50:09 +0530 Subject: [PATCH] Gets an epub generated - no image support yet --- .editorconfig | 20 ++++++++++++++++++++ build/.gitignore | 2 ++ generate.py | 25 +++++++++++++++++++++++++ requirements.txt | 9 +++++++++ 4 files changed, 56 insertions(+) create mode 100644 .editorconfig create mode 100644 build/.gitignore create mode 100644 generate.py create mode 100644 requirements.txt diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ddd4148 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,20 @@ +; +; Global Editor Config for Adaptive Labbers +; +; This is an ini style configuration. See http://editorconfig.org/ for more information on this file. +; +; Top level editor config. +root = true +; Always use Unix style new lines with new line ending on every file and trim whitespace +[*] +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +; Python: PEP8 defines 4 spaces for indentation +[*.py] +indent_style = space +indent_size = 4 +; Salt state files, YAML format, 2 spaces +[*.sls, *.yaml, *.yml] +indent_style = space +indent_size = 2 \ No newline at end of file diff --git a/build/.gitignore b/build/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/build/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/generate.py b/generate.py new file mode 100644 index 0000000..0b9b4b1 --- /dev/null +++ b/generate.py @@ -0,0 +1,25 @@ +from bs4 import BeautifulSoup +import pypub + +epub = pypub.Epub('Site Reliability Engineering') + +def setup_toc(): + soup = BeautifulSoup(open('./html/index.html'), 'html.parser') + links = soup.select('.content a ') + for link in links: + print(link['href']) + add_chapter_file(link['href'], link.get_text()) + + epub.create_epub('./build') + +def add_chapter_file(href, title): + file_path = href.replace('/sre/book/', 'html/') + + with open(file_path, 'r') as f: + contents = f.read() + chapter_soup = BeautifulSoup(contents, 'html.parser') + chapter_html = chapter_soup.select_one('.content').prettify("utf-8") + chapter = pypub.create_chapter_from_string(chapter_html, url=None, title=title) + epub.add_chapter(chapter) + +setup_toc() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6a1043c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +appdirs==1.4.0 +beautifulsoup4==4.5.3 +Jinja2==2.9.5 +MarkupSafe==0.23 +packaging==16.8 +pyparsing==2.1.10 +pypub==1.4 +requests==2.13.0 +six==1.10.0