From 30ba6079b931e1d3a3622d882a4a02018f6b9c1f Mon Sep 17 00:00:00 2001 From: cheungdaven Date: Wed, 6 Jul 2022 00:14:42 +0000 Subject: [PATCH 01/12] add cambridge latex style --- d2lbook/build.py | 24 ++++--- d2lbook/sphinx.py | 11 +-- d2lbook/sphinx_template.py | 138 +++++++++++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+), 12 deletions(-) diff --git a/d2lbook/build.py b/d2lbook/build.py index f37cf11..c0890ff 100644 --- a/d2lbook/build.py +++ b/d2lbook/build.py @@ -33,15 +33,20 @@ def build(): parser.add_argument('commands', nargs='+', choices=commands) parser.add_argument('--tab', default=None, help='The tab to build, if multi-tab is enabled.') + parser.add_argument('--style', default=None, + help='The pdf style. It supports cambridge style and sphinx default.') args = parser.parse_args(sys.argv[2:]) config = Config(tab=args.tab) builder = Builder(config) for cmd in args.commands: - getattr(builder, cmd)() + if cmd == 'pdf': + getattr(builder, cmd)(args.style) + else: + getattr(builder, cmd)() def _once(func): # An decorator that run a method only once - def warp(self): + def warp(self, *args): name = func.__name__ if self.config.tab: name += '_' + self.config.tab @@ -49,7 +54,10 @@ def warp(self): return full_name = 'd2lbook build ' + name tik = datetime.datetime.now() - func(self) + if ("rst" in name or "pdf" in name) and len(args) > 0: + func(self, args[0]) + else: + func(self) logging.info('=== Finished "%s" in %s', full_name, get_time_diff(tik, datetime.datetime.now())) self.done[name] = True @@ -276,7 +284,7 @@ def slides(self): sd.generate_readme() @_once - def rst(self): + def rst(self, style=None): if self.config.tab == 'all': self.merge() else: @@ -292,7 +300,7 @@ def rst(self): mkdir(os.path.dirname(tgt)) ipynb2rst(src, tgt) # Generate conf.py under rst folder - prepare_sphinx_env(self.config) + prepare_sphinx_env(self.config, style) self._copy_rst() self._copy_resources(self.config.src_dir, self.config.rst_dir) @@ -354,8 +362,8 @@ def linkcheck(self): '-b linkcheck -c', self.config.rst_dir, self.sphinx_opts]) @_once - def pdf(self): - self.rst() + def pdf(self, style=None): + self.rst(style) run_cmd([ 'sphinx-build ', self.config.rst_dir, self.config.pdf_dir, '-b latex -c', self.config.rst_dir, self.sphinx_opts]) @@ -537,7 +545,7 @@ def process_latex(fname, script): f.write('\n'.join(lines)) # Execute custom process_latex script if script: - cmd = "python " + script + " " + fname + cmd = "python3.8 " + script + " " + fname process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() diff --git a/d2lbook/sphinx.py b/d2lbook/sphinx.py index d8afa51..fa350bd 100644 --- a/d2lbook/sphinx.py +++ b/d2lbook/sphinx.py @@ -5,14 +5,17 @@ __all__ = ['prepare_sphinx_env'] -def prepare_sphinx_env(config): - env = SphinxEnv(config) +def prepare_sphinx_env(config, style): + env = SphinxEnv(config, style) env.prepare_env() class SphinxEnv(object): - def __init__(self, config): + def __init__(self, config, style=None): self.config = config - self.pyconf = template.sphinx_conf + if style == "cambridge": + self.pyconf = template.sphinx_conf_cambridge + else: + self.pyconf = template.sphinx_conf def prepare_env(self): self._copy_static_files() diff --git a/d2lbook/sphinx_template.py b/d2lbook/sphinx_template.py index ba930ad..2427593 100644 --- a/d2lbook/sphinx_template.py +++ b/d2lbook/sphinx_template.py @@ -114,6 +114,144 @@ def setup(app): app.add_directive('card', mxtheme.CardDirective) """ +sphinx_conf_cambridge = r""" +import sys +sys.path.insert(0, '..') +sys.path.insert(0, '.') + +project = "TITLE" +copyright = "COPYRIGHT" +author = "AUTHOR" +release = "RELEASE" + +extensions = [EXTENSIONS] + +templates_path = ['_templates'] +exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] +master_doc = 'INDEX' +numfig = True +numfig_secnum_depth = 2 +math_numfig = True +math_number_all = True + +suppress_warnings = ['misc.highlighting_failure'] +linkcheck_ignore = [r'.*localhost.*'] +linkcheck_timeout = 5 +linkcheck_workers = 20 + +autodoc_default_options = { + 'undoc-members': True, + 'show-inheritance': True, +} + + +html_theme = 'mxtheme' +html_theme_options = { + 'primary_color': 'blue', + 'accent_color': 'deep_orange', + 'header_links': [ + HEADER_LINKS + ], + 'show_footer': False +} +html_static_path = ['_static'] + +html_favicon = 'FAVICON' + +html_logo = 'HTML_LOGO' + +latex_documents = [ + (master_doc, "NAME.tex", "TITLE", + author, 'PT1'), +] + + + +rsvg_converter_args = ['-z', '0.8'] + +latex_engine = 'xelatex' # for utf-8 supports +latex_show_pagerefs = True +latex_show_urls = 'footnote' + +latex_logo = 'LATEX_LOGO' + +latex_elements = { +'papersize':'a4paper,prodtf', +'figure_align': 'htbp', + +'pointsize': '12pt', +'preamble': r''' +\usepackage{graphicx} +\usepackage{booktabs} +\usepackage{amsthm} +\usepackage{color} +\usepackage[figuresright]{rotating} +\usepackage{floatpag} +\rotfloatpagestyle{empty} +\usepackage{makeidx} +\usepackage{subcaption} +\usepackage{natbib} +\usepackage[parfill]{parskip} +\usepackage{titlesec} + +\makeatletter +\let\ps@normal\ps@headings +\let\sphinxthebibliography\thebibliography +\let\sphinxtheindex\theindex + +% So some large pictures won't get the full page +\renewcommand{\floatpagefraction}{.8} + +\protected\def\sphinxcite{\citep} +\geometry{margin=1.65in} + +\titleformat{\section}{\LARGE\centering}{\thesection}% + {0.5em}{}[{\hspace{-1.65in}\raggedleft\includegraphics[width=35pc]{PT1secrule.eps}}] +\titleformat{\subsection}{\Large\centering}% + {\thesubsection}{0.5em}{}[{\color{gray}\titlerule[0.8pt]}] + +\newcommand\cambridge{PT1} +\theoremstyle{plain}% default +\newtheorem{theorem}{Theorem}[chapter] +\newtheorem{lemma}[theorem]{Lemma} +\newtheorem*{corollary}{Corollary} +\theoremstyle{definition} +\newtheorem{definition}[theorem]{Definition} +\newtheorem{condition}[theorem]{Condition} +\newtheorem{example-norules}[theorem]{Example} +\theoremstyle{remark} +\newtheorem*{remark}{Remark} +\newtheorem*{case}{Case} + +\hyphenation{line-break line-breaks docu-ment triangle cambridge + amsthdoc cambridgemods baseline-skip author authors + cambridgestyle en-vir-on-ment polar astron-omers solu-tion} + +\setcounter{tocdepth}{2} + +\hbadness=99999 % or any number >=10000 +\vfuzz=30pt +\hfuzz=30pt +''', +'maketitle':'\\maketitle', +'tableofcontents': '\\tableofcontents', +'fncychap':'', +'makeindex':'\\makeindex' +} + +latex_additional_files = ["latex_style/PT1.cls","latex_style/PT1header.eps", "latex_style/PT1secrule.eps", "latex_style/PT1box.eps", "latex_style/PT1chrule.eps", "latex_style/multind.sty", "latex_style/amsthm.sty", "latex_style/floatpag.sty","latex_style/rotating.sty", "latex_style/myriad-pt1.sty", "latex_style/natbib.sty", "latex_style/sphinx.sty", "latex_style/sphinxlatexstyleheadings.sty", "latex_style/sphinxlatexstylepage.sty", "latex_style/sphinxlatexindbibtoc.sty", "latex_style/sphinxmessages.sty", "latex_style/sphinxlatexobjects.sty", +"latex_style/natbib.dtx"] + +SPHINX_CONFIGS + +def setup(app): + # app.add_js_file('https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js') + app.add_js_file('d2l.js') + app.add_css_file('d2l.css') + import mxtheme + app.add_directive('card', mxtheme.CardDirective) +""" + google_tracker = """ (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ From 8db0e9b5f1a7d7efde548f283431d9edf4a7ed26 Mon Sep 17 00:00:00 2001 From: cheungdaven Date: Fri, 8 Jul 2022 19:58:36 +0000 Subject: [PATCH 02/12] download latex style from given url during compilation --- d2lbook/build.py | 33 +++++++++++++++++++++++++++++++++ d2lbook/sphinx_template.py | 4 ++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/d2lbook/build.py b/d2lbook/build.py index c0890ff..c7aa33a 100644 --- a/d2lbook/build.py +++ b/d2lbook/build.py @@ -8,6 +8,9 @@ import shutil import subprocess import sys +import tarfile +import zipfile +import requests import nbformat import notedown @@ -230,6 +233,34 @@ def _copy_rst(self): for src, tgt in updated_rst: copy(src, tgt) return rst_files + + def _download_extract_latex(self, url, folder='latex_style', sha1_hash=None): + os.makedirs(folder, exist_ok=True) + fname = os.path.join(folder, url.split('/')[-1]) + # Check if hit cache + if os.path.exists(fname) and sha1_hash: + sha1 = hashlib.sha1() + with open(fname, 'rb') as f: + while True: + data = f.read(1048576) + if not data: + break + sha1.update(data) + if sha1.hexdigest() == sha1_hash: + return fname + print(f'Downloading {fname} from {url}...') + r = requests.get(url, stream=True, verify=True) + with open(fname, 'wb') as f: + f.write(r.content) + base_dir = os.path.dirname(folder) + data_dir, ext = os.path.splitext(fname) + if ext == '.zip': + fp = zipfile.ZipFile(fname, 'r') + elif ext in ('.tar', '.gz'): + fp = tarfile.open(fname, 'r') + else: + assert False, 'Only zip/tar files can be extracted.' + fp.extractall(folder) @_once def merge(self): @@ -364,6 +395,8 @@ def linkcheck(self): @_once def pdf(self, style=None): self.rst(style) + if style: + self._download_extract_latex(self.config.pdf['latex_url']) run_cmd([ 'sphinx-build ', self.config.rst_dir, self.config.pdf_dir, '-b latex -c', self.config.rst_dir, self.sphinx_opts]) diff --git a/d2lbook/sphinx_template.py b/d2lbook/sphinx_template.py index 7bf612d..c1029af 100644 --- a/d2lbook/sphinx_template.py +++ b/d2lbook/sphinx_template.py @@ -241,8 +241,8 @@ def setup(app): 'makeindex':'\\makeindex' } -latex_additional_files = ["latex_style/PT1.cls","latex_style/PT1header.eps", "latex_style/PT1secrule.eps", "latex_style/PT1box.eps", "latex_style/PT1chrule.eps", "latex_style/multind.sty", "latex_style/amsthm.sty", "latex_style/floatpag.sty","latex_style/rotating.sty", "latex_style/myriad-pt1.sty", "latex_style/natbib.sty", "latex_style/sphinx.sty", "latex_style/sphinxlatexstyleheadings.sty", "latex_style/sphinxlatexstylepage.sty", "latex_style/sphinxlatexindbibtoc.sty", "latex_style/sphinxmessages.sty", "latex_style/sphinxlatexobjects.sty", -"latex_style/natbib.dtx"] +latex_additional_files = ["latex_style/PT1/PT1.cls","latex_style/PT1/PT1header.eps", "latex_style/PT1/PT1secrule.eps", "latex_style/PT1/PT1box.eps", "latex_style/PT1/PT1chrule.eps", "latex_style/PT1/multind.sty", "latex_style/PT1/amsthm.sty", "latex_style/PT1/floatpag.sty","latex_style/PT1/rotating.sty", "latex_style/PT1/myriad-pt1.sty", "latex_style/PT1/natbib.sty", "latex_style/sphinx.sty", "latex_style/sphinxlatexstyleheadings.sty", "latex_style/sphinxlatexstylepage.sty", "latex_style/sphinxlatexindbibtoc.sty", "latex_style/sphinxmessages.sty", "latex_style/sphinxlatexobjects.sty", +"latex_style/PT1/natbib.dtx"] SPHINX_CONFIGS From a718a8071193b290af9114f748d3ee4afd713a80 Mon Sep 17 00:00:00 2001 From: cheungdaven Date: Fri, 8 Jul 2022 20:22:41 +0000 Subject: [PATCH 03/12] add bibtex_bibfiles --- d2lbook/sphinx_template.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/d2lbook/sphinx_template.py b/d2lbook/sphinx_template.py index c1029af..9a8beba 100644 --- a/d2lbook/sphinx_template.py +++ b/d2lbook/sphinx_template.py @@ -171,6 +171,8 @@ def setup(app): rsvg_converter_args = ['-z', '0.8'] +bibtex_bibfiles = ["BIBFILE"] + latex_engine = 'xelatex' # for utf-8 supports latex_show_pagerefs = True latex_show_urls = 'footnote' From 445ce44d696bff4c8ab6077452e8ade1c342008e Mon Sep 17 00:00:00 2001 From: Shuai Zhang Date: Fri, 15 Jul 2022 14:45:50 -0700 Subject: [PATCH 04/12] Add new features (1) add automatic QR code generation footnote; (2) change code and text font style; (3) reduce the figure size, and fix some style issues. --- d2lbook/sphinx_template.py | 68 +++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/d2lbook/sphinx_template.py b/d2lbook/sphinx_template.py index 9a8beba..4b86f95 100644 --- a/d2lbook/sphinx_template.py +++ b/d2lbook/sphinx_template.py @@ -52,8 +52,6 @@ rsvg_converter_args = ['-z', '0.8'] -bibtex_bibfiles = ["BIBFILE"] - latex_engine = 'xelatex' # for utf-8 supports latex_show_pagerefs = True latex_show_urls = 'footnote' @@ -171,8 +169,6 @@ def setup(app): rsvg_converter_args = ['-z', '0.8'] -bibtex_bibfiles = ["BIBFILE"] - latex_engine = 'xelatex' # for utf-8 supports latex_show_pagerefs = True latex_show_urls = 'footnote' @@ -180,10 +176,10 @@ def setup(app): latex_logo = 'LATEX_LOGO' latex_elements = { -'papersize':'a4paper,prodtf', -'figure_align': 'htbp', - -'pointsize': '12pt', +'papersize':'a4paper,prodtf,twoside', +'figure_align': 'htbp', +'pointsize': '11pt', +'fvset':r'''\fvset{fontsize=\small}''', 'preamble': r''' \usepackage{graphicx} \usepackage{booktabs} @@ -193,26 +189,67 @@ def setup(app): \usepackage{floatpag} \rotfloatpagestyle{empty} \usepackage{makeidx} -\usepackage{subcaption} \usepackage{natbib} \usepackage[parfill]{parskip} \usepackage{titlesec} +\usepackage{multicol} +\protected\def\sphinxcite{\citep} + +% Add bib to TOC +\usepackage[nottoc,numbib]{tocbibind} + +% QR code sidenotes for all footnotes +% Make sure to replace special charactors URL Encoding: https://www.urlencoder.io/learn/ +\usepackage{sidenotes} +\usepackage{marginfix} +\setlength\marginparpush{20pt} +\usepackage{qrcode} +\newcommand{\qrsidenote}[1]{ +\sidenote{ +\qrcode[height=8mm]{#1}} +} +\newcommand{\relaxfootnote}[1][]{} \makeatletter \let\ps@normal\ps@headings \let\sphinxthebibliography\thebibliography \let\sphinxtheindex\theindex +\let\sphinxAtStartFootnote\! +\let\footnote\relaxfootnote +\let\sphinxnolinkurl\qrsidenote % So some large pictures won't get the full page \renewcommand{\floatpagefraction}{.8} -\protected\def\sphinxcite{\citep} -\geometry{margin=1.65in} +% Set the page margin size +\geometry{left=1.9in, right=1.4in, includefoot, bottom=0.5in} +% Section and subsection style \titleformat{\section}{\LARGE\centering}{\thesection}% {0.5em}{}[{\hspace{-1.65in}\raggedleft\includegraphics[width=35pc]{PT1secrule.eps}}] \titleformat{\subsection}{\Large\centering}% {\thesubsection}{0.5em}{}[{\color{gray}\titlerule[0.8pt]}] + +% Code font style, for more font style, visit: https://tug.org/FontCatalogue/ +\setmonofont{Inconsolata} +%\renewcommand\ttfamily{\sffamily} + +% Resize all figures +\let\ORIincludegraphics\includegraphics +\renewcommand{\includegraphics}[2][]{\ORIincludegraphics[scale=0.75,#1]{#2}} +% main text font style +\usepackage{times} + +% Rewrite table of contents +\renewcommand\tableofcontents{\@restonecolfalse + \if@twocolumn\@restonecoltrue\onecolumn\fi + %\AJW@addtocfalse + \chapter*{\contentsname} + %\@starttoc{toc} + %\AJW@addtoctrue + \if@restonecol\twocolumn\fi + \@starttoc{toc} +} \newcommand\cambridge{PT1} \theoremstyle{plain}% default @@ -227,11 +264,12 @@ def setup(app): \newtheorem*{remark}{Remark} \newtheorem*{case}{Case} + \hyphenation{line-break line-breaks docu-ment triangle cambridge amsthdoc cambridgemods baseline-skip author authors cambridgestyle en-vir-on-ment polar astron-omers solu-tion} -\setcounter{tocdepth}{2} +\setcounter{tocdepth}{1} \hbadness=99999 % or any number >=10000 \vfuzz=30pt @@ -243,8 +281,8 @@ def setup(app): 'makeindex':'\\makeindex' } -latex_additional_files = ["latex_style/PT1/PT1.cls","latex_style/PT1/PT1header.eps", "latex_style/PT1/PT1secrule.eps", "latex_style/PT1/PT1box.eps", "latex_style/PT1/PT1chrule.eps", "latex_style/PT1/multind.sty", "latex_style/PT1/amsthm.sty", "latex_style/PT1/floatpag.sty","latex_style/PT1/rotating.sty", "latex_style/PT1/myriad-pt1.sty", "latex_style/PT1/natbib.sty", "latex_style/sphinx.sty", "latex_style/sphinxlatexstyleheadings.sty", "latex_style/sphinxlatexstylepage.sty", "latex_style/sphinxlatexindbibtoc.sty", "latex_style/sphinxmessages.sty", "latex_style/sphinxlatexobjects.sty", -"latex_style/PT1/natbib.dtx"] +latex_additional_files = ["latex_style/PT1/PT1.cls", "latex_style/PT1header.eps", "latex_style/PT1secrule.eps", "latex_style/PT1/PT1box.eps", "latex_style/PT1/PT1chrule.eps", "latex_style/PT1/multind.sty", "latex_style/PT1/amsthm.sty", "latex_style/PT1/floatpag.sty","latex_style/PT1/rotating.sty", "latex_style/PT1/myriad-pt1.sty", "latex_style/PT1/natbib.sty", "latex_style/sphinx.sty", "latex_style/sphinxlatexstyleheadings.sty", "latex_style/sphinxlatexstylepage.sty", "latex_style/sphinxlatexindbibtoc.sty", "latex_style/sphinxmessages.sty", "latex_style/sphinxlatexobjects.sty", +"latex_style/PT1/natbib.dtx", "latex_style/sphinxpackagefootnote.sty", "latex_style/sphinxlatexlists.sty"] SPHINX_CONFIGS @@ -549,7 +587,7 @@ def setup(app): } .mdl-tabs__tab.is-active { - background: rgb(0,0,0,.09); + background: rgb(0,0,0,.09); } """ From 677ad6c392adfe16b22d8ec0ae6031a1671af602 Mon Sep 17 00:00:00 2001 From: Shuai Zhang Date: Fri, 15 Jul 2022 15:38:27 -0700 Subject: [PATCH 05/12] Update build.py --- d2lbook/build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/d2lbook/build.py b/d2lbook/build.py index c7aa33a..087b34c 100644 --- a/d2lbook/build.py +++ b/d2lbook/build.py @@ -578,7 +578,7 @@ def process_latex(fname, script): f.write('\n'.join(lines)) # Execute custom process_latex script if script: - cmd = "python3.8 " + script + " " + fname + cmd = "python " + script + " " + fname process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() From ae451fe6e42f36d26e45485981e32a0882874eb9 Mon Sep 17 00:00:00 2001 From: Aston Zhang <22279212+astonzhang@users.noreply.github.com> Date: Tue, 2 Aug 2022 11:32:46 -0700 Subject: [PATCH 06/12] Update d2lbook/build.py --- d2lbook/build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/d2lbook/build.py b/d2lbook/build.py index 087b34c..f007758 100644 --- a/d2lbook/build.py +++ b/d2lbook/build.py @@ -37,7 +37,7 @@ def build(): parser.add_argument('--tab', default=None, help='The tab to build, if multi-tab is enabled.') parser.add_argument('--style', default=None, - help='The pdf style. It supports cambridge style and sphinx default.') + help='The pdf style. It supports cambridge style (cambridge) and sphinx default (None).') args = parser.parse_args(sys.argv[2:]) config = Config(tab=args.tab) builder = Builder(config) From 60d8dc1df9751c4b9e429f9057579dc1a7a89947 Mon Sep 17 00:00:00 2001 From: Aston Zhang <22279212+astonzhang@users.noreply.github.com> Date: Tue, 2 Aug 2022 15:17:58 -0700 Subject: [PATCH 07/12] Update d2lbook/sphinx_template.py --- d2lbook/sphinx_template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/d2lbook/sphinx_template.py b/d2lbook/sphinx_template.py index 4b86f95..2e79eae 100644 --- a/d2lbook/sphinx_template.py +++ b/d2lbook/sphinx_template.py @@ -51,7 +51,7 @@ ] rsvg_converter_args = ['-z', '0.8'] - +bibtex_bibfiles = ["BIBFILE"] latex_engine = 'xelatex' # for utf-8 supports latex_show_pagerefs = True latex_show_urls = 'footnote' From 187bb8402fe2a3027dac63101975c329271f81f3 Mon Sep 17 00:00:00 2001 From: Aston Zhang <22279212+astonzhang@users.noreply.github.com> Date: Tue, 2 Aug 2022 15:19:40 -0700 Subject: [PATCH 08/12] Update d2lbook/sphinx_template.py --- d2lbook/sphinx_template.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/d2lbook/sphinx_template.py b/d2lbook/sphinx_template.py index 2e79eae..02c0507 100644 --- a/d2lbook/sphinx_template.py +++ b/d2lbook/sphinx_template.py @@ -169,6 +169,8 @@ def setup(app): rsvg_converter_args = ['-z', '0.8'] +bibtex_bibfiles = ["BIBFILE"] + latex_engine = 'xelatex' # for utf-8 supports latex_show_pagerefs = True latex_show_urls = 'footnote' From b2d863056f73846e7f9e3991634c619fc3462204 Mon Sep 17 00:00:00 2001 From: cheungdaven Date: Wed, 3 Aug 2022 21:11:20 +0000 Subject: [PATCH 09/12] use config to invoke cambridge style --- d2lbook/build.py | 26 ++++++++++---------------- d2lbook/sphinx_template.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/d2lbook/build.py b/d2lbook/build.py index f007758..defd54a 100644 --- a/d2lbook/build.py +++ b/d2lbook/build.py @@ -36,20 +36,15 @@ def build(): parser.add_argument('commands', nargs='+', choices=commands) parser.add_argument('--tab', default=None, help='The tab to build, if multi-tab is enabled.') - parser.add_argument('--style', default=None, - help='The pdf style. It supports cambridge style (cambridge) and sphinx default (None).') args = parser.parse_args(sys.argv[2:]) config = Config(tab=args.tab) builder = Builder(config) for cmd in args.commands: - if cmd == 'pdf': - getattr(builder, cmd)(args.style) - else: - getattr(builder, cmd)() + getattr(builder, cmd)() def _once(func): # An decorator that run a method only once - def warp(self, *args): + def warp(self): name = func.__name__ if self.config.tab: name += '_' + self.config.tab @@ -57,10 +52,7 @@ def warp(self, *args): return full_name = 'd2lbook build ' + name tik = datetime.datetime.now() - if ("rst" in name or "pdf" in name) and len(args) > 0: - func(self, args[0]) - else: - func(self) + func(self) logging.info('=== Finished "%s" in %s', full_name, get_time_diff(tik, datetime.datetime.now())) self.done[name] = True @@ -262,6 +254,7 @@ def _download_extract_latex(self, url, folder='latex_style', sha1_hash=None): assert False, 'Only zip/tar files can be extracted.' fp.extractall(folder) + @_once def merge(self): assert self.config.tab == 'all' @@ -315,7 +308,7 @@ def slides(self): sd.generate_readme() @_once - def rst(self, style=None): + def rst(self): if self.config.tab == 'all': self.merge() else: @@ -331,7 +324,7 @@ def rst(self, style=None): mkdir(os.path.dirname(tgt)) ipynb2rst(src, tgt) # Generate conf.py under rst folder - prepare_sphinx_env(self.config, style) + prepare_sphinx_env(self.config) self._copy_rst() self._copy_resources(self.config.src_dir, self.config.rst_dir) @@ -393,10 +386,11 @@ def linkcheck(self): '-b linkcheck -c', self.config.rst_dir, self.sphinx_opts]) @_once - def pdf(self, style=None): - self.rst(style) - if style: + def pdf(self): + self.rst() + if self.config.pdf['style'] is not None: self._download_extract_latex(self.config.pdf['latex_url']) + run_cmd([ 'sphinx-build ', self.config.rst_dir, self.config.pdf_dir, '-b latex -c', self.config.rst_dir, self.sphinx_opts]) diff --git a/d2lbook/sphinx_template.py b/d2lbook/sphinx_template.py index 8e13a8d..5643030 100644 --- a/d2lbook/sphinx_template.py +++ b/d2lbook/sphinx_template.py @@ -255,6 +255,10 @@ def setup(app): \let\footnote\relaxfootnote \let\sphinxnolinkurl\qrsidenote +\sphinxDeclareColorOption{TitleColor}{{rgb}{0,0,0}} +\sphinxDeclareColorOption{InnerLinkColor}{{rgb}{0,0,0}} +\sphinxDeclareColorOption{OuterLinkColor}{{rgb}{0,0,0}} + % So some large pictures won't get the full page \renewcommand{\floatpagefraction}{.8} @@ -311,15 +315,35 @@ def setup(app): \hbadness=99999 % or any number >=10000 \vfuzz=30pt \hfuzz=30pt + +% Defines macros for code-blocks styling +\definecolor{d2lbookOutputCellBackgroundColor}{RGB}{255,255,255} +\definecolor{d2lbookOutputCellBorderColor}{RGB}{0,0,0} +\def\diilbookstyleoutputcell + {\sphinxcolorlet{VerbatimColor}{d2lbookOutputCellBackgroundColor}% + \sphinxcolorlet{VerbatimBorderColor}{d2lbookOutputCellBorderColor}% + \sphinxsetup{verbatimwithframe,verbatimborder=0.5pt}% + }% +% +\definecolor{d2lbookInputCellBackgroundColor}{rgb}{.95,.95,.95} +\def\diilbookstyleinputcell + {\sphinxcolorlet{VerbatimColor}{d2lbookInputCellBackgroundColor}% + \sphinxsetup{verbatimwithframe=false,verbatimborder=0pt}% + }% + ''', +'sphinxsetup': '''verbatimsep=2mm, + VerbatimColor={rgb}{.95,.95,.95}, + VerbatimBorderColor={rgb}{.95,.95,.95}, + pre_border-radius=3pt, + ''', 'maketitle':'\\maketitle', 'tableofcontents': '\\tableofcontents', 'fncychap':'', 'makeindex':'\\makeindex' } -latex_additional_files = ["latex_style/PT1/PT1.cls", "latex_style/PT1header.eps", "latex_style/PT1secrule.eps", "latex_style/PT1/PT1box.eps", "latex_style/PT1/PT1chrule.eps", "latex_style/PT1/multind.sty", "latex_style/PT1/amsthm.sty", "latex_style/PT1/floatpag.sty","latex_style/PT1/rotating.sty", "latex_style/PT1/myriad-pt1.sty", "latex_style/PT1/natbib.sty", "latex_style/sphinx.sty", "latex_style/sphinxlatexstyleheadings.sty", "latex_style/sphinxlatexstylepage.sty", "latex_style/sphinxlatexindbibtoc.sty", "latex_style/sphinxmessages.sty", "latex_style/sphinxlatexobjects.sty", -"latex_style/PT1/natbib.dtx", "latex_style/sphinxpackagefootnote.sty", "latex_style/sphinxlatexlists.sty"] +latex_additional_files = ["latex_style/PT1/PT1.cls", "latex_style/PT1header.eps", "latex_style/PT1secrule.eps", "latex_style/PT1/PT1box.eps", "latex_style/PT1/PT1chrule.eps", "latex_style/PT1/multind.sty", "latex_style/PT1/amsthm.sty", "latex_style/PT1/floatpag.sty","latex_style/PT1/rotating.sty", "latex_style/PT1/myriad-pt1.sty", "latex_style/PT1/natbib.sty", "latex_style/sphinxlatexstyleheadings.sty", "latex_style/sphinxlatexstylepage.sty", "latex_style/sphinxlatexindbibtoc.sty", "latex_style/sphinxmessages.sty", "latex_style/sphinxlatexobjects.sty", "latex_style/PT1/natbib.dtx", "latex_style/sphinxpackagefootnote.sty", "latex_style/sphinxlatexlists.sty"] SPHINX_CONFIGS From ea5b5e07eea6c3d075b217974ba06ae5acca85d2 Mon Sep 17 00:00:00 2001 From: Shuai Zhang Date: Wed, 3 Aug 2022 15:38:59 -0700 Subject: [PATCH 10/12] add latex_url and style under pdf configuration --- d2lbook/config_default.ini | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/d2lbook/config_default.ini b/d2lbook/config_default.ini index dc3d855..8e030f5 100644 --- a/d2lbook/config_default.ini +++ b/d2lbook/config_default.ini @@ -136,6 +136,11 @@ latex_logo = # Bibtext bibfile bibfile = +# Specify the url of external latex resources. +latex_url = + +# Specify the latex style. We now support "cambridge" and the defaut sphinx style. +style = [library] From cc9d457bf75a1056021aa7ea01de99d656dc4ef9 Mon Sep 17 00:00:00 2001 From: Shuai Zhang Date: Wed, 3 Aug 2022 15:41:19 -0700 Subject: [PATCH 11/12] Update build.py --- d2lbook/build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/d2lbook/build.py b/d2lbook/build.py index defd54a..16a7b36 100644 --- a/d2lbook/build.py +++ b/d2lbook/build.py @@ -388,7 +388,7 @@ def linkcheck(self): @_once def pdf(self): self.rst() - if self.config.pdf['style'] is not None: + if self.config.pdf['style'] == 'cambridge': self._download_extract_latex(self.config.pdf['latex_url']) run_cmd([ From 27c6cd5c22c744aee41f4fb545f88f18032ae69c Mon Sep 17 00:00:00 2001 From: Shuai Zhang Date: Wed, 3 Aug 2022 15:53:43 -0700 Subject: [PATCH 12/12] Update sphinx.py --- d2lbook/sphinx.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/d2lbook/sphinx.py b/d2lbook/sphinx.py index 70c7ff4..e92824b 100644 --- a/d2lbook/sphinx.py +++ b/d2lbook/sphinx.py @@ -5,14 +5,14 @@ __all__ = ['prepare_sphinx_env'] -def prepare_sphinx_env(config, style): - env = SphinxEnv(config, style) +def prepare_sphinx_env(config): + env = SphinxEnv(config) env.prepare_env() class SphinxEnv(object): - def __init__(self, config, style=None): + def __init__(self, config): self.config = config - if style == "cambridge": + if self.config.pdf['style'] == 'cambridge': self.pyconf = template.sphinx_conf_cambridge else: self.pyconf = template.sphinx_conf