def add_prev_next_links(filename2contents, only_for=None): new_one = OrderedDict() for filename, contents in list(filename2contents.items()): if only_for and not filename in only_for: continue id_prev = contents.attrs[ATTR_PREV] a_prev = Tag(name='a') a_prev.attrs['href'] = '#' + str(id_prev) a_prev.attrs['class'] = CLASS_LINK_PREV a_prev.append('prev') id_next = contents.attrs[ATTR_NEXT] a_next = Tag(name='a') a_next.attrs['href'] = '#' + str(id_next) a_next.attrs['class'] = CLASS_LINK_NEXT a_next.append('next') S = Tag(name='div') S.attrs['class'] = ['super'] nav1 = Tag(name='div') add_class(nav1, 'navigation') if id_prev: nav1.append(a_prev.__copy__()) if id_next: nav1.append(a_next.__copy__()) spacer = Tag(name='div') spacer.attrs['style'] = 'clear:both' nav1.append(spacer) add_class(contents, 'main-section-for-page') contents2 = contents S.append(contents2) from .source_info_imp import get_main_header actual_id = get_main_header(contents2) if False: # just checking e = contents2.find(id=actual_id) if e is not None: pass else: logger.error('not found %r' % actual_id) S.attrs['id'] = actual_id contents2.insert(0, nav1.__copy__()) contents2.append(nav1.__copy__()) new_one[filename] = S return new_one
def go(context, worker_i, num_workers, data, mathjax, preamble, output_dir, assets_dir, add_toc_if_not_existing, extra_panel_content, permalink_prefix=None, output_crossref=None, only_refs=False): res = AugmentedResult() soup = bs_entire_document(data) # extract the main toc if it is there with timeit("Extracting main toc"): main_toc = soup.find(id=MCDPManualConstants.MAIN_TOC_ID) if main_toc is None: if add_toc_if_not_existing: # logger.info('Generating TOC because it is not there') tocg = generate_toc(soup) main_toc = bs(tocg).ul main_toc.attrs['class'] = 'toc' # XXX: see XXX13 assert main_toc is not None substituting_empty_links(main_toc, raise_errors=False, res=res, extra_refs=soup) else: msg = 'Could not find main toc (id #%s)' % MCDPManualConstants.MAIN_TOC_ID res.note_error(msg) main_toc = Tag(name='div') main_toc.append('TOC NOT FOUND') else: main_toc = main_toc.__copy__() if 'id' in main_toc.attrs: del main_toc.attrs['id'] # XXX: this is not the place to do it mark_toc_links_as_errored(main_toc, soup) body = soup.html.body with timeit("split_in_files"): filename2contents = split_in_files(body) id2filename = get_id2filename(filename2contents) res.set_result(id2filename) if output_crossref is not None: from mcdp_docs.mcdp_render_manual import write_crossref_info context.comp(write_crossref_info, data=data, id2filename=id2filename, output_crossref=output_crossref, permalink_prefix=permalink_prefix) if only_refs: logger.debug('Skipping rest because only_refs') return res with timeit("add_prev_next_links"): filename2contents = add_prev_next_links(filename2contents) with timeit("preparing assets dir"): if not os.path.exists(output_dir): try: os.makedirs(output_dir) except: pass with timeit("creating link.html and link.js"): linkbase = 'link.html' # do not change (it's used by http://purl.org/dth) linkbasejs = 'link.js' lb = create_link_base(id2filename) write_data_to_file(str(lb), os.path.join(output_dir, linkbase), quiet=True) linkjs = create_link_base_js(id2filename) write_data_to_file(str(linkjs), os.path.join(output_dir, linkbasejs), quiet=True) if preamble is not None: if preamble.endswith('.tex'): # XXX preamble = open(preamble).read() ids_to_use = [] for k in list(id2filename): if not 'autoid' in k: ids_to_use.append(k) ids_to_use = sorted(ids_to_use) pointed_to = [] for k in ids_to_use: f = id2filename[k] if not f in pointed_to: pointed_to.append(f) # data = ",".join(pointed_to) head0 = soup.html.head if True: context.comp(remove_spurious, output_dir, list(filename2contents)) with timeit('main_toc copy'): main_toc0 = main_toc.__copy__() main_toc0_s = str(main_toc0) asset_jobs = [] for i, (filename, contents) in enumerate(filename2contents.items()): if i % num_workers != worker_i: continue with timeit('main_toc copy hack'): main_toc = bs(main_toc0_s).ul assert main_toc is not None # Trick: we add the main_toc, and then ... (look below) with timeit('make_page'): add_home_link = 'index.html' not in filename2contents html = make_page(contents, head0, main_toc, extra_panel_content, add_home_link=add_home_link) with timeit("direct job"): result = only_second_part(mathjax, preamble, html, id2filename, filename) # ... we remove it. In this way we don't have to copy it every time... main_toc.extract() fn = os.path.join(output_dir, filename) h = get_md5(result)[:8] r = context.comp(extract_assets_from_file, result, fn, assets_dir, job_id='%s-%s-assets' % (filename, h)) asset_jobs.append(r) update_refs_('toc.html', main_toc, id2filename) out_toc = os.path.join(output_dir, 'toc.html') write_data_to_file(str(main_toc), out_toc, quiet=True) return context.comp(wait_assets, res, asset_jobs)