Example #1
0
def get_line(filename, lineno):
    """Return the lineno'th line of a file."""
    with common.slashfix_open(filename, 'r') as f:
        for lineno2, line in enumerate(f, start=1):
            if lineno == lineno2:
                return line
    raise ValueError("%s is less than %d lines long" % (filename, lineno))
Example #2
0
def get_line(filename, lineno):
    """Return the lineno'th line of a file."""
    with common.slashfix_open(filename, 'r') as f:
        for lineno2, line in enumerate(f, start=1):
            if lineno == lineno2:
                return line
    raise ValueError("%s is less than %d lines long" % (filename, lineno))
Example #3
0
def needs_stripping(file):
    with common.slashfix_open(file, 'r') as f:
        for line in f:
            line = line.rstrip('\n')
            if line != fix(line):
                return True
    return False
def update_end(filename, end):
    """Add *** and end to a file if it doesn't have them already.

    filename should be relative to the toplevel using / as a path
    separator.
    """
    end = '\n***\n\n' + end
    with common.slashfix_open(filename, 'r') as f:
        content = f.read()
    if content.endswith(end):
        # No need to do anything.
        print("  Has correct end:", filename)
        return

    if '\n***\n' in content:
        # We need to remove the old ending first.
        print("  Removing old end:", filename)
        where = content.index('\n***\n')
        with common.slashfix_open(filename, 'w') as f:
            f.write(content[:where])

    print("  Adding end:", filename)
    with common.slashfix_open(filename, 'a') as f:
        f.write(end)
Example #5
0
def update_end(filename, end):
    """Add *** and end to a file if it doesn't have them already.

    filename should be relative to the toplevel using / as a path
    separator.
    """
    end = '\n***\n\n' + end
    with common.slashfix_open(filename, 'r') as f:
        content = f.read()
    if content.endswith(end):
        # No need to do anything.
        print("  Has correct end:", filename)
        return

    if '\n***\n' in content:
        # We need to remove the old ending first.
        print("  Removing old end:", filename)
        where = content.index('\n***\n')
        with common.slashfix_open(filename, 'w') as f:
            f.write(content[:where])

    print("  Adding end:", filename)
    with common.slashfix_open(filename, 'a') as f:
        f.write(end)
Example #6
0
def find_titles(filename):
    """Read titles of a markdown file and return a list of them."""
    result = []

    with common.slashfix_open(filename, 'r') as f:
        for line in f:
            if line.startswith('```'):
                # it's a code block, let's skip to the end of it to
                # avoid detecting comments as titles
                while f.readline().rstrip() != '```':
                    pass
            if line.startswith('#'):
                # found a title
                result.append(common.header_link(line.lstrip('#').strip()))

    return result
Example #7
0
def find_titles(filename):
    """Read titles of a markdown file and return a list of them."""
    result = []

    with common.slashfix_open(filename, 'r') as f:
        for line in f:
            if line.startswith('```'):
                # it's a code block, let's skip to the end of it to
                # avoid detecting comments as titles
                while f.readline().rstrip() != '```':
                    pass
            if line.startswith('#'):
                # found a title
                result.append(common.header_link(line.lstrip('#').strip()))

    return result
Example #8
0
def main():
    print("Searching and checking links...")
    broken = 0
    total = 0
    for path in common.get_markdown_files():
        with common.slashfix_open(path, 'r') as f:
            for match, lineno in common.find_links(f):
                text, target = match.groups()
                status = check(path, target)
                if status != "ok":
                    # The .group(0) is not perfect, but it's good enough.
                    print("  file %s, line %d: %s" % (path, lineno, status))
                    print("    " + match.group(0))
                    print()
                    broken += 1
                total += 1
    print("%d/%d links seem to be broken." % (broken, total))
def main():
    if pygments is None:
        print("Pygments isn't installed. You can install it like this:")
        print()
        print(">>> import pip")
        print(">>> pip.main(['install', '--user', 'Pygments'])")
        print()
        print("You can also continue without Pygments, but the code examples")
        print("will not be in color.")
        if not common.askyesno("Continue without pygments?"):
            print("Interrupt.")
            return

    if os.path.exists('html'):
        if not common.askyesno("html exists. Do you want to remove it?"):
            print("Interrupt.")
            return
        if os.path.isdir('html'):
            shutil.rmtree('html')
        else:
            os.remove('html')

    print("Generating HTML files...")
    for markdownfile in common.get_markdown_files():
        htmlfile = posixpath.join('html', fix_filename(markdownfile))
        print(' ', markdownfile, '->', htmlfile)
        with common.slashfix_open(markdownfile, 'r') as f:
            markdown = f.read()
        renderer = TutorialRenderer()
        body = mistune.markdown(markdown, renderer=renderer)
        html = HTML_TEMPLATE.format(title=renderer.title, body=body)
        with mkdir_slashfix_open(htmlfile, 'w') as f:
            print(html, file=f)

    print("Copying other files...")
    shutil.copytree('images', os.path.join('html', 'images'))
    shutil.copy('LICENSE', os.path.join('html', 'LICENSE'))

    print("\n*********************\n")
    print("Ready! The files are in the html directory.")
    print("Go to html and double-click index.html to read the tutorial.")
    print()
    if common.askyesno("Do you want to view the tutorial now?", default=False):
        print("Opening the tutorial...")
        webbrowser.open(os.path.join('html', 'index.html'))
Example #10
0
def find_links(this_file):
    """Read links of a markdown file.

    Return a list of (target, title, lineno) pairs where title can be None.
    """
    result = []

    with common.slashfix_open(this_file, 'r') as f:
        for match, lineno in common.find_links(f):
            target = match.group(2)
            if '#' in target:
                file, title = target.split('#', 1)
                if not file:
                    # link to this file, [blabla](#hi)
                    file = posixpath.basename(this_file)
            else:
                file = target
                title = None

            result.append((file, title, lineno))

    return result
Example #11
0
def find_links(this_file):
    """Read links of a markdown file.

    Return a list of (target, title, lineno) pairs where title can be None.
    """
    result = []

    with common.slashfix_open(this_file, 'r') as f:
        for match, lineno in common.find_links(f):
            target = match.group(2)
            if '#' in target:
                file, title = target.split('#', 1)
                if not file:
                    # link to this file, [blabla](#hi)
                    file = posixpath.basename(this_file)
            else:
                file = target
                title = None

            result.append((file, title, lineno))

    return result
Example #12
0
def main():
    desc = ("Create HTML files of the tutorial.\n\n"
            "The files have light text on a dark background by "
            "default, and you can edit html-style.css to change that.")
    if pygments is not None:
        desc += (" Editing the style file doesn't change the colors of the "
                 "code examples, but you can use the --pygments-style "
                 "option. Search for 'pygments style gallery' online or see "
                 "https://help.farbox.com/pygments.html to get an idea of "
                 "what different styles look like.")

    parser = argparse.ArgumentParser(
        description=wrap_text(desc),
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument(
        '-o',
        '--outdir',
        default='html',
        help="write the HTML files here, defaults to %(default)r")
    if pygments is not None:
        parser.add_argument('--pygments-style',
                            metavar='STYLE',
                            default=TutorialStyle,
                            choices=list(pygments.styles.get_all_styles()),
                            help=("the Pygments color style (see above), "
                                  "defaults to a custom style"))
    args = parser.parse_args()

    if pygments is None:
        print("Pygments isn't installed. You can install it like this:")
        print()
        print(">>> import pip")
        print(">>> pip.main(['install', '--user', 'pygments'])")
        print()
        print("You can also continue without Pygments, but the code examples")
        print("will not be colored.")
        if not common.askyesno("Continue without pygments?"):
            print("Interrupt.")
            return
        args.pygments_style = None

    if os.path.exists(args.outdir):
        if not common.askyesno(
                "%s exists. Do you want to remove it?" % args.outdir):
            print("Interrupt.")
            return
        if os.path.isdir(args.outdir):
            shutil.rmtree(args.outdir)
        else:
            os.remove(args.outdir)

    print("Generating HTML files...")
    for markdownfile in common.get_markdown_files():
        fixed_file = fix_filename(markdownfile)
        htmlfile = posixpath.join(args.outdir, fixed_file)
        print('  %-30.30s  -->  %-30.30s' % (markdownfile, htmlfile), end='\r')

        with common.slashfix_open(markdownfile, 'r') as f:
            markdown = f.read()
        renderer = TutorialRenderer(args.pygments_style)
        body = mistune.markdown(markdown, renderer=renderer)
        stylefile = posixpath.relpath('style.css',
                                      posixpath.dirname(fixed_file))

        html = HTML_TEMPLATE.format(
            title=renderer.title,
            body=body,
            stylefile=stylefile,
        )
        with mkdir_slashfix_open(htmlfile, 'w') as f:
            print(html, file=f)
    print()

    print("Copying other files...")
    shutil.copytree('images', os.path.join(args.outdir, 'images'))
    shutil.copy('LICENSE', os.path.join(args.outdir, 'LICENSE.txt'))
    shutil.copy('html-style.css', os.path.join(args.outdir, 'style.css'))

    print("\n*********************\n")
    print("Ready! The files are in %r." % args.outdir)
    print("You can go there and double-click index.html to read the tutorial.")
    print()
    if common.askyesno("Do you want to view the tutorial now?", default=False):
        print("Opening the tutorial...")
        webbrowser.open(os.path.join(args.outdir, 'index.html'))