예제 #1
0
def convert_wiki(source, dest, dest_project_id):
    if overwrite and (method == 'direct'):
        dest.clear_wiki_attachments(dest_project_id)

    exclude_authors = [a.strip() for a in config.get('wiki', 'exclude_authors').split(',')]
    target_directory = config.get('wiki', 'target-directory')
    server = xmlrpclib.MultiCall(source)
    for name in source.wiki.getAllPages():
        info = source.wiki.getPageInfo(name)
        if (info['author'] not in exclude_authors):
            page = source.wiki.getPage(name)
            print("Page %s:%s" % (name, info))
            if (name == 'WikiStart'):
                name = 'home'
            converted = trac2down.convert(page, os.path.dirname('/wikis/%s' % name))
            if method == 'direct':
                for attachment in source.wiki.listAttachments(name):
                    print(attachment)
                    binary_attachment = source.wiki.getAttachment(attachment).data
                    try:
                        attachment_path = dest.create_wiki_attachment(dest_project_id, users_map[info['author']], convert_xmlrpc_datetime(info['lastModified']), attachment, binary_attachment)
                    except KeyError:
                        attachment_path = dest.create_wiki_attachment(dest_project_id, default_user, convert_xmlrpc_datetime(info['lastModified']), attachment, binary_attachment)
                    attachment_name = attachment.split('/')[-1]
                    converted = converted.replace(r'](%s)' % attachment_name, r'](%s)' % os.path.relpath(attachment_path, '/namespace/project/wiki/page'))
            trac2down.save_file(converted, name, info['version'], info['lastModified'], info['author'], target_directory)
예제 #2
0
def convert_wiki(source, dest, dest_project_id):
    if overwrite and (method == 'direct'):
        dest.clear_wiki_attachments(dest_project_id)

    exclude_authors = [a.strip() for a in config.get('wiki', 'exclude_authors').split(',')]
    target_directory = config.get('wiki', 'target-directory')
    server = xmlrpclib.MultiCall(source)
    for name in source.wiki.getAllPages():
        info = source.wiki.getPageInfo(name)
        if (info['author'] not in exclude_authors):
            page = source.wiki.getPage(name)
            print "Page %s:%s" % (name, info)
            if (name == 'WikiStart'):
                name = 'home'
            converted = trac2down.convert(page, os.path.dirname('/wikis/%s' % name))
            if method == 'direct':
                for attachment in source.wiki.listAttachments(name):
                    print attachment
                    binary_attachment = source.wiki.getAttachment(attachment).data
                    try:
                        attachment_path = dest.create_wiki_attachment(dest_project_id, users_map[info['author']], convert_xmlrpc_datetime(info['lastModified']), attachment, binary_attachment)
                    except KeyError:
                        attachment_path = dest.create_wiki_attachment(dest_project_id, default_user, convert_xmlrpc_datetime(info['lastModified']), attachment, binary_attachment)
                    attachment_name = attachment.split('/')[-1]
                    converted = converted.replace(r'](%s)' % attachment_name, r'](%s)' % os.path.relpath(attachment_path, '/namespace/project/wiki/page'))
            trac2down.save_file(converted, name, info['version'], info['lastModified'], info['author'], target_directory)
    """Add double [[...]] around wikilinks (given as `targets`)

    Most prosaic method possible - simply loop over the explicit list
    of targets.  Tries to be careful in avoiding code blocks, but
    will still be fooled by inline code
    """
    lines = []
    is_code_block = False
    for line in text.split('\n'):
        # not blockquote?
        if not line.startswith('    '):
            if line.startswith("````"):
                is_code_block = not is_code_block
            if not is_code_block:
                for target in targets:
                    line = line.replace(target, f"[[{target}]]")
            # line = re.sub(r'\!(([A-Z][a-z0-9]+){2,})', r'[[\1]]', line)
        lines.append(line)
    return "\n".join(lines)


# Process all the files in the input folder
for p in input_path.glob("*"):
    # Open file from the dump of the trac database and convert it
    with open(p) as f:
        text = trac2down.convert(f.read(), ".")
        text = fixup_wikilinks(text, wikinames)
    # Save the converted file with a ".md" extension to output folder
    # The 3rd, 4th, 5th arguments to this function are unused
    trac2down.save_file(text, p.name, None, None, None, save_path)
예제 #4
0
def convert_wiki(source, dest):
    exclude_authors = [a.strip() for a in config.get('wiki', 'exclude_authors').split(',')]
    target_directory = config.get('wiki', 'target-directory')

    if wiki_override_page:
        pages = [wiki_override_page]
    else:
        pages = source.wiki.getAllPages()

    i = 0
    for name in pages:
        i += 1
        info = source.wiki.getPageInfo(name)
        if info == 0:
            raise Exception("No page named %s could be found" % name)

        if info['author'] in exclude_authors:
            continue

        page = source.wiki.getPage(name)
        print("[%d/%d] Page %s:%s" % (i, len(pages), name, info))
        if name == 'WikiStart':
            name = 'home'

        sanitized_name = name.replace('/', '-').lower()
        upload_prefix = 'uploads/%s' % sanitized_name
        old_attachment_prefix = '/attachment/wiki/%s' % name
        old_raw_attachment_prefix = '/raw-attachment/wiki/%s' % name
        converted = trac2down.convert(
            page,
            os.path.dirname('/wikis/%s' % name),
            wiki_upload_prefix=upload_prefix,
            old_attachment_prefix=old_attachment_prefix,
            old_raw_attachment_prefix=old_raw_attachment_prefix
        )

        if method == 'direct' and not ignore_wiki_attachments:
            files_not_linked_to = []

            for attachment_filename in source.wiki.listAttachments(name):
                binary_attachment = source.wiki.getAttachment(attachment_filename).data
                attachment_name = attachment_filename.split('/')[-1]
                sanitized_attachment_name = attachment_name \
                    .replace(' ', '_') \
                    .replace('(', '') \
                    .replace(')', '')
                attachment_directory = os.path.join(target_directory, 'uploads', sanitized_name)

                dest.save_wiki_attachment(attachment_directory, sanitized_attachment_name, binary_attachment)
                converted = converted.replace(r'%s/%s)' % (sanitized_name, attachment_filename),
                                                r'%s/%s)' % (sanitized_name, sanitized_attachment_name))
                if '%s/%s)' % (upload_prefix, sanitized_attachment_name) not in converted:
                    files_not_linked_to.append(sanitized_attachment_name)

                print('  ' + sanitized_attachment_name)

            if len(files_not_linked_to) > 0:
                print '  %d non-linked attachments detected, manually adding to generated Markdown' % len(files_not_linked_to)
                converted += '\n\n'
                converted += '##### Attached files:\n'
                for file_name in files_not_linked_to:
                    converted += '- [%s](uploads/%s/%s)\n' % (file_name, sanitized_name, file_name)

        trac2down.save_file(converted, name, info['version'], info['lastModified'], info['author'], target_directory)