Pi66

merodi
A markdown-based static site generator.
git clone https://git.pi66.xyz/merodi

src/init_project.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
from os import path, getcwd, makedirs

from .modules import Cache, Config, Project, Tree, Webview, Extras
from .log import *
from .errors import fatal
from .templates import *
from .fileops import *

def init_config_struct(project_name:str) -> Config:
    """Build a Config with default values for a newly initialized project."""
    return Config(
        project=Project(
            name        = project_name,
            version     = "0.1.0",
            description = "Add your description here",
        ),
        tree = Tree(
            markdown     = "src/md",
            static       = "src/static",
            templates    = "src/templates",
            draft_dest   = "build/draft",
            release_dest = "build/release",
            plugins      = "src/plugins",
        ),
        webview = Webview(
            host        = "localhost",
            port        = 8866,
            dev_tools   = False,
            html_path   = "/",
            static_path = "/static"
        ),
        extras = Extras(
            highlight = "monokai"
        ),
        cache = Cache(
            hash = ".hash.cache"
        )
    )

def render_config(config:Config) -> str:
    return f"""[project]
name        = "{config.project.name}"
version     = "{config.project.version}"
description = "{config.project.description}"

[tree]
markdown     = "{config.tree.markdown}"
static       = "{config.tree.static}"
templates    = "{config.tree.templates}"
draft_dest   = "{config.tree.draft_dest}"
release_dest = "{config.tree.release_dest}"
plugins      = "{config.tree.plugins}"

[webview]
host        = "{config.webview.host}"
port        = {config.webview.port}
dev_tools   = {str(config.webview.dev_tools).lower()}
html_path   = "{config.webview.html_path}"
static_path = "{config.webview.static_path}"

[extras]
highlight = "{config.extras.highlight}"

[cache]
hash = "{config.cache.hash}"
"""

def check_not_already_initialized(file_path:str) -> None:
    """raise FileExistsError when config file exists"""
    if path.exists(file_path):
        raise Exception("Project already initialized: `config.toml` already exists")

def write_default_content(tree:Tree) -> None:
    """ Write default content """
    md_file = path.join(tree.markdown, "index.md")
    write_file(md_file,MARKDOWN_CONTENT )

    html_file = path.join(tree.templates, "layout.html")
    write_file(html_file,HTML_CONTENT )

    css_file = path.join(tree.static, "style.css")
    write_file(css_file,CSS_CONTENT )

    plugins_file = path.join(tree.plugins, "main.py")
    write_file(plugins_file,PLUGINS_CONTENT )

def write_config_file(project_path, config_content):
    makedirs(project_path, exist_ok=True)
    config_path = path.join(project_path, "config.toml")
    check_not_already_initialized(config_path)

    # Writing config.toml
    write_file(config_path, config_content)


def init(project_path):
    try:
        project_path = project_path if project_path else getcwd()
        config = init_config_struct(path.basename(project_path) ) 
        default_config_content = render_config(config)
        config.tree = resolve_tree_paths(project_path, config.tree)
        write_config_file(project_path, default_config_content)

        create_tree_dirs(config.tree)
        write_default_content(config.tree)
        info("Project initialized.")
    except Exception as e:
        fatal(e, f"Initialization failed: {e}")