summaryrefslogtreecommitdiff
path: root/src/pyssg/pyssg.py
blob: b076abb65a073749be317b8b7a93021259eed20b (plain)
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
import os
from argparse import ArgumentParser, Namespace

from .database import Database
from .template import Template
from .builder import build_static_site


def get_options() -> Namespace:
    parser = ArgumentParser(prog='pyssg',
                            description='''Static Site Generator that reads
                            Markdown files and creates HTML files.''')
    parser.add_argument('-s', '--src',
                        default='src',
                        type=str,
                        help='''src directory; handmade files, templates and
                        metadata directory; defaults to 'src' ''')
    parser.add_argument('-d', '--dst',
                        default='dst',
                        type=str,
                        help='''dst directory; generated (and transfered html)
                        files; defaults to 'dst' ''')
    parser.add_argument('-i', '--init',
                        action='store_true',
                        help='''initializes the dir structure, templates,
                        as well as the 'src' and 'dst' directories''')
    parser.add_argument('-b', '--build',
                        action='store_true',
                        help='''generates all html files and passes over
                        existing (handmade) ones''')

    return parser.parse_args()


def main() -> None:
    opts: dict[str] = vars(get_options())
    src: str = opts['src']
    dst: str = opts['dst']

    if opts['init']:
        try:
            os.mkdir(src)
            os.makedirs(os.path.join(dst, 'tag'))
        except FileExistsError:
            pass

        template: Template = Template(src)
        template.write()
        return

    if opts['build']:
        db: Database = Database(os.path.join(src, '.files'))

        build_static_site(src, dst, db)

        db.write()
        return