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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#!/usr/bin/env python

import argparse
import codecs
import copy
import dateparser
import glob
import logging
import multiprocessing
import os
import pytz
import re
import shutil
import sys
import time

from datetime import datetime
from bs4 import BeautifulSoup
from jinja2 import Environment, FileSystemLoader
from markupsafe import Markup
from slugify import slugify

import markdown
import orjson
import pypandoc
import sass

from btbytes.gitstuff import get_commits, get_file_history
from btbytes.jfilters import mydateformat

utc = pytz.UTC

DOMAIN = "https://www.btbytes.com"
BUILD = os.path.realpath(__file__)
BASE = os.path.dirname(os.path.realpath(__file__))
DEST = os.path.join(BASE, "output")
SRC = os.path.join(BASE, "content")
TDIR = "_layouts"
loader = FileSystemLoader(searchpath=SRC)
env = Environment(loader=loader)
env.filters["mydateformat"] = mydateformat


# Create the file handler
file_handler = logging.FileHandler("sitebuild.log")
file_handler.setLevel(logging.WARNING)

# Create the console handler
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.INFO)

# Create formatters and add them to the handlers
file_formatter = logging.Formatter(
    "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
console_formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s")
file_handler.setFormatter(file_formatter)
console_handler.setFormatter(console_formatter)

# Get the root logger and add the handlers
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)  # Ensure everything is captured at root level
logger.addHandler(file_handler)
logger.addHandler(console_handler)

# # Now you can log messages!
# logger.debug("This is a debug message, visible in the console")
# logger.info("This is an info message, visible in the console")
# logger.warning("This is a warning, visible in console and file")
# logger.error("This is an error, visible in console and file")


def sort_by_tag_count(value):
    return sorted(value, key=lambda x: len(x))


env.filters["sort_by_tag_count"] = sort_by_tag_count


def mdict(f, s):
    """
    $name: $content -- date, last-modified,
    path, url, title,keywords, date
    """
    m = s.find_all("meta")
    d = {}
    d["path"] = f
    d["url"] = urlfor(f)
    ts = s.find("title")
    if ts:
        d["title"] = ts.text.strip()
    h1s = s.find_all("h1")
    if len(h1s) > 0:
        h1 = h1s[0]
        d["h1"] = h1.text
    for x in m:
        k = x.attrs.get("http-equiv")
        k = k or x.attrs.get("name")
        v = x.attrs.get("content")
        d[k] = v
    for k in [None, "viewport"]:
        if k in d:
            d.pop(k)
    if "keywords" in d.keys():
        d["keywords"] = [k.strip() for k in d["keywords"].split(",")]
    if "date" in d.keys():
        try:
            ddate = dateparser.parse(d["date"])
            ddate = ddate.replace(tzinfo=utc)
            d["date"] = ddate.strftime("%Y-%m-%dT%H:%M:%S")
            # d['date'] = dateparser.parse(d['date'])
            # d['date'] = d['date'].replace(tzinfo=utc)
        except Exception as e:
            logger.warning("unable to process date in: %s - %s - %s" % (f, d.keys(), e))
    return d


def build(target, sources):
    """
    if the `target` file's modification time is newer than
    any of the `sources` one's then return true
    """
    if not os.path.exists(target):
        return True
    tmtime = os.path.getmtime(target)
    stimes = [os.path.getmtime(s) for s in sources]
    return any([tmtime < s for s in stimes])


def render_pandoc(content, md):
    filters = ["pandoc/filters.lua"]
    pdoc_args = [
        # "--standalone",
        "--citeproc",  # render citations
        "--bibliography",
        "content/bib/nbrefs.bib",
        "--csl",
        "content/csl/jcsl.csl",
    ]
    bib = md.Meta.get("bib", None)
    csl = md.Meta.get("csl", None)
    number_sections = md.Meta.get("number_sections", False)
    toc = md.Meta.get("toca", False)
    if bib and not csl:  # default style file
        csl = ["csl/jcsl.csl"]
    if number_sections:
        pdoc_args += ["--number-sections"]
    if toc:
        pdoc_args += ["--toc"]
    try:
        output = pypandoc.convert_text(
            content, "html", format="md", filters=filters, extra_args=pdoc_args
        )
        return output
    except Exception as e:
        logger.warning("render_pandoc_exc: %s -- %s" % (md.Meta, e))
        sys.exit(0)


def render_md(html, md):
    env.filters["markdown"] = lambda text: Markup(md.convert(text))
    if md.Meta.get("layout"):
        tfile = os.path.join(TDIR, md.Meta.get("layout"))
    elif md.Meta.get("kind") == "log":
        tfile = os.path.join(TDIR, "notebook.htm")
    elif md.Meta.get("kind") == "post":
        tfile = os.path.join(TDIR, "post.html")
    elif md.Meta.get("kind") == "til":
        tfile = os.path.join(TDIR, "til.html")
    elif md.Meta.get("kind") == "note":
        tfile = os.path.join(TDIR, "semantic.html")
    elif md.Meta.get("kind") == "readingnote":
        tfile = os.path.join(TDIR, "readingnote.htm")
    else:
        tfile = os.path.join(TDIR, "notebook.htm")
    template = env.get_template(tfile)
    pagemeta = {"body": html, "meta": md.Meta, "now": datetime.utcnow()}
    content = template.render(**pagemeta)
    return content


def make_directory(outfile):
    a, b = os.path.split(outfile)
    outdir = os.path.join(a)
    os.makedirs(outdir, exist_ok=True)


def minify_content(content):
    soup = BeautifulSoup(content, "lxml")  # html.parser
    return str(soup)


def render_to_html(infile, outfile, html, md):
    if md.Meta.get("draft"):
        return
    uri = f"/{outfile[outfile.find('output')+7:]}"
    url = f"{DOMAIN}{uri}"
    md.Meta["url"] = url
    content = render_md(html, md)
    content = minify_content(content)
    make_directory(outfile)
    with codecs.open(outfile, "w", "utf-8") as f:
        logger.debug(f"generating: {url}")
        f.write(content)


def convert_scss_to_css(srcfile, outfile):
    make_directory(outfile)
    with open(outfile, "w") as f:
        f.write(sass.compile(filename=srcfile, output_style="compressed"))


def link_to_blocks(text):
    """
    Replace ^abcdef (hexadecimal number of length 6 characters)
    with <a id="abcdef" class="block">#</a>
    """
    pattern = r"(\^[0-9a-fA-F]{6})"
    replaced_text = re.sub(
        pattern,
        lambda match: f'<span id="{match.group(1)}" class="block">#</span>',
        text,
    )
    return replaced_text


def replace_link_to_blocks(text):
    """
    Replace a wikilink like [[webdesign#^cb2493]] with a link to the block
    like this: [webdesign](/webdesign.html#cb2493)
    """

    def replace_link(match):
        parts = match.group(1).split("#")
        if len(parts) == 1:
            return f"[{parts[0]}](/{parts[0].replace(' ', '-')}.html)"
        else:
            return f"[{parts[0]}](/{parts[0].replace(' ', '-')}.html#{parts[1]})"

    pattern = r"\[\[(.*?)\]\]"
    replaced_text = re.sub(pattern, replace_link, text)
    return replaced_text


def replace_links(text):
    # Define the regex pattern to match [[Text]]
    pattern = r"\[\[(.*?)\]\]"

    # Replace occurrences of the pattern with the desired format
    replaced_text = re.sub(
        pattern,
        lambda match: "["
        + match.group(1)
        + "](/"
        + match.group(1).replace(" ", "-")
        + ".html){.wikilink}",
        text,
    )

    return replaced_text


def preprocess(mdcontent):
    # mdcontent = link_to_blocks(mdcontent)
    mdcontent = replace_links(mdcontent)
    return mdcontent


def extract_metadata_md(mdfile):
    """
    This is only used for extracting metadata & not for markdown processing.
    Markdown parsing is handle by Pandoc.
    """
    fpath = mdfile.replace(BASE + "/", "")
    with open(mdfile, "r") as f:
        md = markdown.Markdown(extensions=["full_yaml_metadata"])
        content = f.read()
        html = md.convert(
            content
        )  # this is required to trigger the metadata extraction
        content = preprocess(content)
        if not md.Meta:
            logger.warning(
                f"{'*' * 10 }MetaError. File=({mdfile}) -> md.Meta=({md.Meta})"
            )
            md.Meta = dict()
            md.Meta["title"] = fpath.split("/")[-1].replace(".md", "")
        md.Meta["generator"] = "pandoc"
        html = render_pandoc(content, md)
        created, modified = get_file_history(BASE, fpath)
        md.Meta["created"] = (
            md.Meta.get("date", None)
            or created
            or datetime.fromtimestamp(os.path.getctime(mdfile))
        )
        md.Meta["modified"] = (
            md.Meta.get("updated", None)
            or modified
            or datetime.fromtimestamp(os.path.getmtime(mdfile))
        )
        return html, md


def get_target_path(mdfile, a=".md", b=".html"):
    basename = os.path.basename(mdfile)
    justfile = basename.split(".")[0]
    target = mdfile.replace(SRC, DEST).replace(a, b)
    slug = slugify(justfile, lowercase=False)
    target = target.replace(justfile, slug)
    return target


def urlfor(fpath):
    return fpath.replace(DEST, "")


def extract_html_metadata(infile):
    with open(infile) as f:
        return BeautifulSoup(f, features="html.parser")


def get_git_messages(gitlog, lead):
    """Get interesting git messages"""

    commits = []
    # combined = {}
    for c in gitlog:
        if lead in c["title"]:
            c["title"] = c["title"].replace(lead, "")
            c["date"] = dateparser.parse(c["date"]).strftime("%Y-%m-%dT%H:%M:%S")
            commits.append(c)
    for c in commits:
        data = [{"title": c["title"], "message": c["message"]}]
        if c["date"] in combined.keys():
            combined["date"] = c["date"].extend(data)
        else:
            c["date"] = data
    return commits


def get_all_markdowns():
    dirs = [
        "",
        "book",
        "certifications",
        "code",
        "learning",
        "links",
        "log",
        "log/**",
        "misc",
        "note",
        "posts",
        "readingnotes",
        "stopwatch",
        "til",
        "weekly",
        "farming",
        "pycon2010",
    ]
    mds = [f for d in dirs for f in glob.glob(os.path.join(SRC, d, "*.md"))]
    return mds


def process_markdown_file(filename):
    target = get_target_path(filename)
    if build(target, [filename]):
        html, md = extract_metadata_md(filename)
        render_to_html(filename, target, html, md)


def process_markdown_files(markdowns):
    logger.info("processing %s markdown files" % (len(markdowns)))
    pool = multiprocessing.Pool(processes=4)
    pool.starmap(process_markdown_file, zip(markdowns))
    pool.close()
    pool.join()


def process_less_files():
    """convert .scss files to .css"""
    logger.info("processing less files")
    scssfiles = glob.glob(os.path.join(SRC, "css", "*.scss"))
    for f in scssfiles:
        target = get_target_path(f, ".scss", ".css")
        convert_scss_to_css(f, target)


def remove_filepaths_by_prefix(allfiles, exclusion_prefixes):
    """Removes file paths from a list if they start with any of the provided prefixes.

    Args:
        alllists: A list of file paths.
        exclusion_prefixes: A list of prefixes to exclude.

    Returns:
        A new list with the excluded file paths removed.
    """

    filtered_list = [
        filepath
        for filepath in allfiles
        if not any(filepath.startswith(prefix) for prefix in exclusion_prefixes)
    ]
    return filtered_list


exclusion_prefixes = [
    ".obsidian",
    "_layouts",
    "_obstemplates",
    "_scripts",
    "_templater",
]


def copy_the_rest(markdowns):
    """Copy the rest"""
    logger.info("copy the rest")
    allfiles = glob.glob(SRC + "/**", recursive=True)
    allfiles = remove_filepaths_by_prefix(allfiles, exclusion_prefixes)
    rest = set(allfiles) - set(markdowns)

    for r in rest:
        if os.path.isdir(r):
            continue
        target = r.replace(SRC, DEST)
        h, t = os.path.split(target)
        tdir = os.path.join(h)
        if not os.path.isdir(tdir):
            os.makedirs(tdir, exist_ok=True)
        if build(target, [BUILD, r]):
            shutil.copyfile(r, target)


def extract_article_body(soup):
    article = soup.find("article")
    return article


def generate_sitemeta():
    logger.debug("sitemeta/start   %s %s" % (time.strftime("%H:%M:%S"), time.time()))
    af = glob.glob(DEST + "/**/*.html", recursive=True)
    af = [a for a in af if "tweetstorms" not in a]
    af = [a for a in af if "whyd" not in a]
    af = [a for a in af if "/files/" not in a]
    logger.debug("sitemeta/extrct0 %s %s" % (time.strftime("%H:%M:%S"), time.time()))
    allmeta = [(a, extract_html_metadata(a)) for a in af]
    logger.info("sitemeta/extrct1 %s %s" % (time.strftime("%H:%M:%S"), time.time()))
    allmeta = [mdict(fa, s) for fa, s in allmeta]
    logger.info("sitemeta/allmeta %s %s" % (time.strftime("%H:%M:%S"), time.time()))
    posts = [
        d
        for d in allmeta
        if d.get("kind") == "post"
        and d.get("date")
        and (
            "show_in_archive" in d.get("keywords", [])
            or "weekly0" not in d.get("keywords", [])
        )
    ]
    sorted_posts = sorted(posts, key=lambda kv: kv.get("date"), reverse=True)
    logger.info(f"sorted_posts: {len(sorted_posts)}")
    for sp in sorted_posts[:10]:
        path = sp["path"]
        body = extract_article_body(extract_html_metadata(path))
        sp["body"] = str(body)

    with open(os.path.join(DEST, "sorted_posts.json"), "wb") as f:
        f.write(orjson.dumps(sorted_posts))

    weeklies = [
        d
        for d in allmeta
        if (
            d.get("kind") == "post"
            and d.get("date")
            and "weekly" in d.get("keywords", [])
        )
    ]
    logs = [d for d in allmeta if (d.get("kind") == "log" and d.get("date"))]
    sorted_weeklies = sorted(weeklies, key=lambda kv: kv.get("date"), reverse=True)
    sorted_logs = sorted(logs, key=lambda kv: kv.get("date"), reverse=True)

    notebooks = [d for d in allmeta if d.get("kind") == "notebook"]
    sorted_notebooks = sorted(
        notebooks, key=lambda kv: kv.get("last-modified", kv.get("date")), reverse=True
    )
    tils = [d for d in allmeta if d.get("kind") == "til"]
    sorted_tils = sorted(tils, key=lambda kv: kv.get("date"), reverse=True)
    logger.debug(
        "generate_sitemeta/sorted  %s %s" % (time.strftime("%H:%M:%S"), time.time())
    )
    anything = [
        d
        for d in allmeta
        if d.get("kind") in ["post", "link", "log", "note", "notebook"]
        and d.get("date")
    ]
    sorted_any = sorted(
        anything, key=lambda kv: kv.get("last-modified", kv.get("date")), reverse=True
    )
    tags = {}
    for d in allmeta:
        kws = d.get("keywords")
        if kws:
            for t in kws:
                if t in tags:
                    tags[t].append(d)
                else:
                    tags[t] = [d]
    logger.info(
        "generate_sitemeta/tags    %s %s" % (time.strftime("%H:%M:%S"), time.time())
    )
    with open(os.path.join(DEST, "allmeta.json"), "wb") as allmetaf:
        allmetaf.write(orjson.dumps(allmeta))
    sitemeta = {
        "sorted_posts": sorted_posts,
        "sorted_notebooks": sorted_notebooks,
        "sorted_weeklies": sorted_weeklies,
        "sorted_tils": sorted_tils,
        "sorted_logs": sorted_logs,
        "now": datetime.now(),
        "changes": get_commits(BASE, PREFIX="CHANGE"),
        "news": get_commits(BASE, PREFIX="NEW"),
        "sitetags": tags,
        "sorted_any": sorted_any,
    }
    with open(os.path.join(DEST, "sitemeta.json"), "wb") as f:
        f.write(
            orjson.dumps(
                sitemeta,
                option=orjson.OPT_NAIVE_UTC
                | orjson.OPT_INDENT_2
                | orjson.OPT_APPEND_NEWLINE,
            )
        )
    with open(os.path.join(DEST, "sorted_posts.json"), "wb") as f:
        f.write(
            orjson.dumps(
                sorted_posts,
                option=orjson.OPT_NAIVE_UTC
                | orjson.OPT_INDENT_2
                | orjson.OPT_APPEND_NEWLINE,
            )
        )
    logger.info(
        "generate_sitemeta/gendict %s %s" % (time.strftime("%H:%M:%S"), time.time())
    )
    return sitemeta


def render_feed_page(sitemeta):
    logger.info("rendering feed page")
    page = SRC + "/feed.jinja"
    tp = page.replace(SRC, "")
    template = env.get_template(tp)
    content = template.render(**sitemeta)
    content = minify_content(content)
    pg = os.path.join(SRC, page)
    t = get_target_path(pg, a=".jinja", b=".xml")

    with open(t, "w") as f:
        logger.debug(f"FEED page: {DOMAIN}/{t[t.find('output')+7:]}")
        f.write(content)


def find_jinja2_files(directory):
    jinja2_files = []
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith(".jinja2"):
                jinja2_files.append(os.path.join(root, file))
    return jinja2_files


def render_site_pages(sitemeta):
    dpages = glob.glob(SRC + "/*.jinja2", recursive=True)
    logger.info(f"Rendering {len(dpages)} site pages")
    md = markdown.Markdown()
    env.filters["markdown"] = lambda text: Markup(md.convert(text))
    for page in dpages:
        tp = page.replace(SRC, "")
        template = env.get_template(tp)
        content = template.render(**sitemeta)

        pg = os.path.join(SRC, page)
        if pg.find("posts.jinja2") > -1:
            t = get_target_path(pg, a=".jinja2", b=".rss")
        elif pg.find("sitemap.xml.jinja2") > -1:
            t = get_target_path(pg, a=".jinja2", b="")
        else:
            t = get_target_path(pg, a=".jinja2")
            content = minify_content(content)
        with open(t, "w") as f:
            logger.debug(f"dynamic page: {DOMAIN}/{t[t.find('output')+7:]}")
            f.write(content)


def main():
    parser = argparse.ArgumentParser(description="Process the site")
    parser.add_argument("--rebuild", action="store_true")  # not using
    parser.add_argument("--full", action="store_true")
    args = parser.parse_args()
    start = time.time()
    markdowns = get_all_markdowns()
    process_markdown_files(markdowns)
    process_less_files()
    now = time.time()
    copy_the_rest(markdowns)
    now1 = time.time()
    logger.info(f"time taken to copy: {now1 - now}")
    logger.info(f"time taken from start: {now1 - start}")

    if args.full:
        logger.info("doing a full build...")
        now2 = time.time()
        sitemeta = generate_sitemeta()
        logger.info("time taken to sitemeta: %.2f" % (time.time() - now2,))
        logger.info("rendering site pages.")
        render_site_pages(sitemeta)
    logger.info(f"finish. total time taken: {time.time() - start}")


if __name__ == "__main__":
    main()