V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
whenov
V2EX  ›  分享创造

分享自用的静态博客生成脚本

  •  
  •   whenov · 2020-08-01 00:16:18 +08:00 · 1888 次点击
    这是一个创建于 1335 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现在的静态博客生成器都太复杂了,大多数功能我都不需要,所以就自己写了几行脚本。有相同需求的朋友可以参考。

    支持 LaTeX 公式,主题是 GitHub 风格。

    使用

    执行 make,可将当前目录下的 Markdown 文件转换为 HTML 文件,并生成目录 index.html 。

    编译时依赖

    Pandoc

    运行时依赖

    MathJax, github-markdown-css

    代码

    page.tpl:

    <!DOCTYPE html>
    <title>$title$</title>
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/4.0.0/github-markdown.min.css">
    <style>
    .markdown-body {
      max-width: 960px;
      margin: 20px auto;
    }
    </style>
    
    <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {inlineMath: [['$$','$$'], ['\\(','\\)']]}
    });
    </script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML' async></script>
    
    <article class="markdown-body">
    $body$
    </article>
    
    

    Makefile:

    index.html : $(patsubst %.md,%.html,$(wildcard *.md))
    	ls -1t *.html | \
    	xargs grep -E --exclude=index.html --max-count=1 "<h1>.+</h1>" | \
    	sed -E "s/(.+)\.html:<h1>(.+)<\/h1>/- [\2](\/\1)/" | \
    	pandoc --standalone --template=page.tpl --metadata=title:"Untitled Site" --output=$@ --from=commonmark
    
    %.html : %.md
    	pandoc --standalone --template=page.tpl --metadata=title:$(basename $@) --output=$@ --from=commonmark $<
    
    5 条回复    2020-08-01 22:35:05 +08:00
    yujiang
        1
    yujiang  
       2020-08-01 09:45:07 +08:00 via Android
    hexo
    whenov
        2
    whenov  
    OP
       2020-08-01 10:02:33 +08:00   ❤️ 1
    @yujiang 个人使用的工具,还是希望能简单易懂。所以我的电脑上用的都是 suckless 那一套( suckless.org

    开发做的越多,越喜欢 DTSTTCPW:Do the simplest thing that could possibly work
    wolong
        3
    wolong  
       2020-08-01 18:42:33 +08:00
    请问脚本在哪啊
    whenov
        4
    whenov  
    OP
       2020-08-01 18:53:42 +08:00
    @wolong 就在页面上啊?一个 html 模板加一个 Makefile
    wolong
        5
    wolong  
       2020-08-01 22:35:05 +08:00
    @whenov 原来是要装一个 Pandoc,我懂了,开始我以为是 node 什么的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2650 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 15:49 · PVG 23:49 · LAX 08:49 · JFK 11:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.