IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    用markdown制作简单的个人主页

    SparkandShine发表于 2016-09-29 21:53:09
    love 0

    学校为每个研究者提供一个空间,用于存放个人学术主页,但只能是静态页面。搜了一下,没找到自己满意的模板。后来就想,用markdown编辑好,再转换成html。事实上,这样做还有一个好处,主页可以在不同设备上显示得很好。动手弄了一个,效果还不错,欢迎围观:Qiankun SU at INP-ENSEEIHT.

    1. 使用Python

    使用pypandoc,示例代码如下:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import pypandoc
    
    # With an input file: it will infer the input format from the filename
    output = pypandoc.convert_file('index.md', 'html', outputfile='index-2.html')
    

    这样做,只是简单地将markdown转换成html,我的markdown文档包含一些法语字符,不能正常显示,需要手动在html文件指定字符编码,即在html文件头部添加如下一行:

    encoding = '<meta charset="utf-8">'
    

    2. 使用命令行

    直接在命令行使用pandoc命令,使用-s选项为输出文档添加header和footer,举例如下:

    # An example 
    pandoc -f markdown index.md -t html -Ss > index.html
    
    # Parameters
    -f format
    Specify input format
    
    -t format
    Specify output format
    
    -S, --smart
    Produce typographically correct output, converting straight quotes to curly quotes, --- to em-dashes, -- to en-dashes, and ... to  ellipses. Nonbreaking spaces are inserted after certain abbreviations, such as "Mr."
    
    -s, --standalone
    Produce output with an appropriate header and footer (e.g.  a standalone HTML, LaTeX, TEI, or RTF file, not a fragment). This option is set automatically for pdf, epub, epub3, fb2, docx, and odt output.
    

    这样就会在生成的html文档添加如下内容:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <meta http-equiv="Content-Style-Type" content="text/css" />
      <meta name="generator" content="pandoc" />
      <link rel="icon" href="logo_inpt.png">
      <title>Qiankun SU at INP-ENSEEIHT</title> 
      <style type="text/css">code{white-space: pre;}</style>
    </head>
    <body>
    ....
    </body>
    </html>
    

    再自己手动添加title和icon,举例如下:

    <link rel="icon" href="logo_inpt.png">
    <title>Qiankun SU at INP-ENSEEIHT</title>
    

    3. 转换bibtex

    比如我一篇论文的bibtex如下:

    @INPROCEEDINGS{su2015xor, 
    author={Q. Su and K. Jaffres-Runser and G. Jakllari and C. Poulliat}, 
    booktitle={2015 IEEE/CIC International Conference on Communications in China (ICCC)}, 
    title={XOR network coding for data mule delay tolerant networks}, 
    year={2015}, 
    pages={1-6}, 
    keywords={Base stations;Delays;Network coding;Protocols;TV;Throughput;Urban areas;Delay tolerant networks;XOR;data mules;network coding;village communication networks}, 
    doi={10.1109/ICCChina.2015.7448634}, 
    month={Nov},}
    

    用上述的方法转换得到的html,得到的是一行,可读性差。使用hard_line_breaks选项,把段落内的newlines转换成换行,而不是空格,举例如下:

    pandoc -f markdown+hard_line_breaks MSWiM16.md -t html -Ss > MSWiM16.html
    
    # hard_line_breaks
    Extension: hard_line_breaks
    Causes all newlines within a paragraph to be interpreted as hard line breaks instead of spaces.
    

    至此,一个简洁的个人学术主页就有了:-)



沪ICP备19023445号-2号
友情链接