(concat_text, docs, title_map, absent_docs)
return "\n".join(lines)
Change document links to anchor links on the concatenated text

    The original Markdown docs are linked to each other based on file
    names. However, once they are concatenated, the original file links do not
    work anymore. This function fixes the problem by changing the file links
    into the corresponding in-document anchor links, based on the document's
    title heading.

    The other problem is that not all markdown docs are included on the
    compiled pdf version. This function removes links to the docs are not
    present in the concatenated text.
Change document links to anchor links on the concatenated text
Change document links to anchor links on the concatenated text
def fix_links(concat_text, docs, title_map, absent_docs):
    """Change document links to anchor links on the concatenated text

    The original Markdown docs are linked to each other based on file
    names. However, once they are concatenated, the original file links do not
    work anymore. This function fixes the problem by changing the file links
    into the corresponding in-document anchor links, based on the document's
    title heading.

    The other problem is that not all markdown docs are included on the
    compiled pdf version. This function removes links to the docs are not
    present in the concatenated text.

    """
    # Replace all document links with the corresponding anchor link
    for doc in docs:
        # Convert title to the corresponding markdown anchor link
        anchor_link = title_map[doc].lower().replace(" ", "-")

        # Replace direct links
        original_link = "({})".format(doc)
        new_link = "(#{})".format(anchor_link)
        concat_text = concat_text.replace(original_link, new_link)

        # Fix links pointing to a particular subsection
        concat_text = concat_text.replace("({}#".format(doc), "(#")

    # Replace links to absent docs
    lines = concat_text.splitlines()
    for doc in absent_docs:
        original_link = "({})".format(doc)
        for i, line in enumerate(lines):
            if original_link in line:
                match = re.search(r"\[(.*?)\]\(" + doc + r"\)", line)
                lines[i] = line.replace(match.group(0), match.group(1))

    return "\n".join(lines)
def fix_links ( concat_text , docs , title_map , absent_docs ) : # Replace all document links with the corresponding anchor link for doc in docs : # Convert title to the corresponding markdown anchor link anchor_link = title_map [ doc ] . lower ( ) . replace ( " " , "-" ) # Replace direct links original_link = "({})" . format ( doc ) new_link = "(#{})" . format ( anchor_link ) concat_text = concat_text . replace ( original_link , new_link ) # Fix links pointing to a particular subsection concat_text = concat_text . replace ( "({}#" . format ( doc ) , "(#" ) # Replace links to absent docs lines = concat_text . splitlines ( ) for doc in absent_docs : original_link = "({})" . format ( doc ) for i , line in enumerate ( lines ) : if original_link in line : match = re . search ( r"\[(.*?)\]\(" + doc + r"\)" , line ) lines [ i ] = line . replace ( match . group ( 0 ) , match . group ( 1 ) ) return "\n" . join ( lines )