Ejemplo n.º 1
0
def create():
    parser = argparse.ArgumentParser(description="Create a new Jupyter Book")
    parser.add_argument(
        "name", help="The name of your Jupyter Book "
                     "(your book template will be placed in a "
                     "folder of this name)")
    parser.add_argument("--out-folder", default='.',
                        help="The location where your book will be placed")
    parser.add_argument("--license", default=None,
                        help="A path to a LICENSE.md file if you "
                             "have already created one")
    parser.add_argument("--content-folder", default=None,
                        help="A path to a folder that holds your book content")
    parser.add_argument("--toc", default=None,
                        help="A path to a yaml file that contains a"
                             " Table of Contents for your Jupyter Book."
                             " This will overwrite parts of the book "
                             "template's default toc.yml configuration")
    msg = ("A path to a configuration YAML file that contains "
           "configuration for your Jupyter Book. This will overwrite"
           " parts of the book template's default _config.yml configuration")
    parser.add_argument("--config", default=None,
                        help=msg)
    msg = ("A path to a CSS file that defines some custom CSS rules for"
           " your book")
    parser.add_argument("--custom-css", default=None, help=msg)
    parser.add_argument("--custom-js", default=None,
                        help="A path to a JS file that defines some "
                             "custom CSS rules for your book")
    parser.add_argument("--extra-files", default=None, nargs="+",
                        help="A list of extra files / folders to copy"
                             " into your book's directory")
    parser.add_argument("--overwrite", default=False, action="store_true",
                        help="Whether to overwrite a pre-existing book"
                             " if it exists")
    msg = ("Whether to build the book with demo content instead of your"
           " own content")
    parser.add_argument("--demo", default=False, action="store_true",
                        help=msg)
    parser.add_argument("--verbose", default=True, action="store_true",
                        help="Whether to display output information. [yes/no]")
    args = parser.parse_args(sys.argv[2:])

    ###############################################
    # Default values and arguments
    path_out = op.join(args.out_folder, args.name)
    content_folder = args.content_folder
    toc = args.toc
    config = args.config
    demo = args.demo
    license = args.license
    custom_css, custom_js = args.custom_css, args.custom_js
    extra_files = args.extra_files
    verbose = args.verbose
    overwrite = args.overwrite

    new_book(path_out, content_folder, toc,
             license, custom_css, custom_js, config, extra_files,
             demo, verbose, overwrite)
Ejemplo n.º 2
0
def test_config_update(tmpdir):
    path_out = op.join(tmpdir.dirpath(), 'tmp_test')
    path_config = op.join(this_folder, 'configs', 'config_update.yml')
    new_name = "test2"

    new_book(op.join(path_out, new_name), config=path_config,
             toc=path_toc, content_folder=path_content,
             license=path_license)

    # Config files
    with open(path_config, 'r') as ff:
        old_config = yaml.load(ff)

    with open(op.join(path_out, new_name, '_config.yml'), 'r') as ff:
        new_config = yaml.load(ff)

    for ii in old_config.keys():
        if ii not in ["jupyter_book_version"]:
            assert old_config[ii] == new_config[ii]

    # If we succeed, remove the tmpdir
    tmpdir.remove()
Ejemplo n.º 3
0
def test_round_trip(tmpdir):
    path_config = op.join(this_folder, 'configs', 'config_simple.yml')
    path_out = op.join(tmpdir.dirpath(), 'tmp_test')

    # Custom CSS and JS code
    path_js = op.join(path_test_book, "my_js.js")
    path_css = op.join(path_test_book, "my_css.css")

    # Run the create command
    new_name = "test"
    new_book(path_out=op.join(path_out, new_name),
             config=path_config,
             toc=path_toc,
             content_folder=path_content,
             custom_js=path_js,
             custom_css=path_css,
             extra_files=[
                 op.join(path_test_book, 'foo', 'baz.txt'),
                 op.join(path_test_book, 'foo', 'you'),
                 op.join(path_test_book, "requirements.txt"),
                 op.join(path_test_book, "_bibliography")
             ],
             license=path_license)

    # Table of contents
    old_toc = read(path_toc)
    new_toc = read(op.join(path_out, new_name, '_data', 'toc.yml'))
    assert old_toc == new_toc

    # Config files
    with open(path_config, 'r') as ff:
        old_config = yaml.load(ff)
    with open(op.join(path_out, new_name, '_config.yml'), 'r') as ff:
        new_config = yaml.load(ff)

    for ii in old_config.keys():
        if ii not in ["jupyter_book_version"]:
            assert old_config[ii] == new_config[ii]

    # License
    old_license = read(path_license)
    new_license = read(op.join(path_out, 'test', 'content', 'LICENSE.md'))
    assert old_license == new_license

    # Content
    for ifolder, _, ifiles in os.walk(path_content):
        for ifile in ifiles:
            basename = op.basename(ifile)
            # Only check the text files we care about since reading in other files is trickier
            if 'LICENSE.md' in basename or all(
                    ii not in basename for ii in ['.md', '.ipynb', '.html']):
                continue

            old_content = read(op.join(ifolder, ifile))
            new_content = read(
                op.join(path_out, 'test', 'content', ifolder, basename))
            assert old_content == new_content

    # CSS and JS
    assert file_contents_equal(
        path_js, op.join(path_out, "test", "assets", "custom", "custom.js"))
    assert file_contents_equal(
        path_css, op.join(path_out, "test", "assets", "custom", "custom.css"))

    # Extra files
    assert op.exists(op.join(path_out, "test", "baz.txt"))
    assert op.exists(op.join(path_out, "test", "you", "bar.txt"))

    # This should raise an error because the folder exists now
    with pytest.raises(CalledProcessError):
        cmd = [
            "jupyter-book", "create", new_name, "--config", path_config,
            "--toc", path_toc, "--content-folder", path_content, "--license",
            path_license, "--out-folder", path_out
        ]
        run(cmd, check=True)

    # If we succeed, remove the tmpdir
    tmpdir.remove()