def translate(self, filename: str): src_fn = os.path.join(self.repo_dir, filename) fns = glob.glob(src_fn) if not len(fns): logging.warn('Not found ' + src_fn) return if len(fns) > 1: for fn in fns: self.translate(os.path.relpath(fn, self.repo_dir)) return src_fn = fns[0] filename = os.path.relpath(src_fn, self.repo_dir) basename, ext = os.path.splitext(filename) origin_tgt_fn = os.path.join(self.config.src_dir, basename + '_origin' + ext) tgt_fn = os.path.join(self.config.src_dir, filename) if os.path.exists(tgt_fn): logging.warn(f'File {tgt_fn} already exists, skip translation.') return logging.info(f'Write original text into {origin_tgt_fn}') utils.mkdir(os.path.dirname(origin_tgt_fn)) with open(origin_tgt_fn, 'w') as f: with open(src_fn, 'r') as f2: f.write(f2.read()) if self.translator and ext == '.md': self.translator.translate_markdown(src_fn, tgt_fn) logging.info(f'Write translated results into {tgt_fn}') else: if not os.path.exists(tgt_fn): with open(tgt_fn, 'w') as f: logging.info(f'Create an empty file {tgt_fn}')
def generate(self, nb: notebooknode.NotebookNode, output_fn: str): """Get all slide blocks and write to file.""" nb = _generate_slides(nb) if not nb: return nb['metadata'].update({ 'language_info': { 'name': 'python' }, 'celltoolbar': 'Slideshow', 'rise': { "autolaunch": True, "enable_chalkboard": True, "overlay": f"<div class='my-top-right'>{self.config.slides['top_right']}</div><div class='my-top-left'>{self.config.slides['top_left']}</div>" } }) dirname = os.path.dirname(output_fn) utils.mkdir(dirname) with open(output_fn, 'w') as f: f.write(nbformat.writes(nb)) logging.info('Write slides into ' + output_fn) with open(dirname + '/rise.css', 'w') as f: f.write(''' div.text_cell_render.rendered_html { padding: 0.35em 0.1em; } div.code_cell { font-size: 120%; } div.my-top-right { position: absolute; right: 5%; top: 1em; font-size: 2em; } div.my-top-left { position: absolute; left: 5%; top: 1em; font-size: 2em; } ''')