#!/usr/bin/env python3 import os import sys import markdown from mdx_gfm import GithubFlavoredMarkdownExtension import weasyprint import re import logging logging.basicConfig(format='%(levelname)s: %(message)s' ,stream=sys.stderr, level=logging.INFO) LOGGER = logging.getLogger('preprocess') # sort the file order def sort_func(x): # place "papers/" at the end (like an appendix) try: x.index('%sdoc%s' % (os.path.sep, os.path.sep)) except ValueError: return 'z' # place readmes at the start of each section try: rm = x.index('README.md') return x[:rm] + '0' except ValueError: return x # make the links work in-pdf def fix_links(match): # images if os.path.splitext(match.group(2))[-1] == '.png': return '[%s](%s)' % ( match.group(1), os.path.join(os.path.split(my_file)[0], match.group(2)) ) # urls to other files BASE_URL = 'https://github.com/tildearrow/furnace/tree/master/' if match.group(2).startswith(BASE_URL): file_path = match.group(2).split(BASE_URL)[-1] if os.path.splitext(file_path)[-1] == '': file_path += '/README.md' return '[%s](#%s)' % ( match.group(1), file_path.replace('/','__') ) # preserve external urls elif match.group(2).startswith('http'): return match.group(0) # fix paths act_path = os.path.split(my_file)[0] + '/' + match.group(2) act_path = os.path.relpath(os.path.abspath(act_path)) return '[%s](#%s)' % ( match.group(1), act_path.replace(os.path.sep,'__') ) def fix_headings(match): return '%s#' % ( match.group(1) ) if __name__ == "__main__": #-- first, prepare the file list --# file_list = [] for i in os.walk('../../doc'): base_dir, subfolders, files = i for file_ in filter(lambda x: x.lower().endswith('.md'), files): file_list.append(os.path.join(base_dir, file_)) #-- then, create the index --# index = '

contents

    ' #-- then, create the document --# html = '' # perform sort file_list.sort(key=sort_func) first = True for my_file in file_list: pageLink = my_file.replace(os.path.sep, "__") if pageLink.endswith("__doc__README.md"): continue with open(my_file, 'r') as md: LOGGER.info("processing file %s" % my_file) data = md.read() # retrieve title pageTitle = data.partition('\n')[0].replace("# ","") if pageLink.endswith("__README.md"): if first: first = False else: index += '
' index += '
  • %s' % ( pageLink, pageTitle, pageLink ) if pageLink.endswith("__README.md"): index += '
      ' else: index += '' # perform link fixing data = re.sub(r'\[(.+?)\]\((.+?)\)', fix_links, data) data = re.sub(r'^\s*(#+)', fix_headings, data, flags=re.MULTILINE) # each file is its own section html +='
      %s
      ' % ( my_file.replace(os.path.sep, "__"), markdown.markdown(data, extensions=['nl2br', 'mdx_breakless_lists', GithubFlavoredMarkdownExtension()]) ) # finish index index += '
  • ' # build html final_html = (''' Furnace Manual

    Furnace
    User Manual

    for version 0.6

    authors

    special thanks to ZoomTen for providing tools which assisted in the production of this document!

    copyright © 2023 tildearrow and other authors.

    this documentation is under the Creative Commons Attribution 3.0 Unported license.

    you may reproduce, modify and/or distribute this documentation provided this copyright notice (including license and attribution) is present and any necessary disclaimers whether modifications have been made.

    this documentation is provided as-is and without warranty of any kind.

    this manual is written for version 0.6 of Furnace.
    it may not necessarily apply to previous or future versions.

    %s
    %s ''' % ( index, html ) ) print(final_html)