emacs org 文档实践

Posted on 2013-01-05 18:35  chenfanyu  阅读(904)  评论(0编辑  收藏  举报

概述

需求:

如何有效的管理文档,这些年来一直在寻找解决方案,曾经试用过onenote,购买过pim之类个人管理类软件. 看完 unix编程艺术,对文档plain化,心生向往,也试用过一阵docbook, 但是docbook过于复杂,又研究过Rest,Markdown. moinmoinwiki,当然还有emacs org. 之后开始使用智能手机,非常迫切需要同步到手机上,于是又用evernote.

这么多方法,但总是不称手,文档也是记录的很凌乱,这个工具有一点,那个工具有一点,这严重违反了一名程序员内心的"道德律",心烦焦躁. 这次是定下决心,准备彻底解决这个问题.

首先是梳理我理想中的文档管理是怎样的:

  1. 所有的文档都是文本化的,甚至包括图形,思维导图也是尽可能文本化.所想即所得
  2. 所有的文档都通过git版本管理
  3. 所有的文档都可以自动同步包括(手机,公司电脑,家庭电脑),支持全文搜索
  4. 不重复编写文档,一处编写,多处发布(导出成pdf,html,发布到blog等)
  5. 支持各种语言的语法高亮显示

思路

针对以上需求,反复比较各种解决方案,比如github托管,octopress,emacs org,docbook,evernote等等,最终决定采用emacs org方式,虽然有些地方还有缺陷,比如移动终端的全文搜索,但是相比其他而已,这个更适合与我

  1. 编辑,采用emacs-orgmode, 各种语言都有很好的支持,orgmode本身的outline非常强大
  2. 采用dropbox同步到各种设备
  3. 采用org export , public发布到blog,导出成pdf,html等

org使用说明

css设置

更改系统缺省设置

org导出成html文件时,会调用org-export-as-html.该函数在没有任何html导出style设置的时候,会使用 org-export-html-style-default 的css样式直接嵌入导出的html中,缺省样式代码在文件 org-html.el中, 要修改样式,可通过设置org-export-html-style-include-default为 t, 然后再设置 org-export-html-style 变量, 这样org-export-html-style的取值会覆盖 org-export-html-style-default的取值.

org css支持选项

p.author            author information, including email
p.date              publishing date
p.creator           creator info, about org mode version
.title              document title
.todo               TODO keywords, all not-done states
.done               the DONE keywords, all states that count as done
.WAITING            each TODO keyword also uses a class named after itself
.timestamp          timestamp
.timestamp-kwd      keyword associated with a timestamp, like SCHEDULED
.timestamp-wrapper  span around keyword plus timestamp
.tag                tag in a headline
._HOME              each tag uses itself as a class, "@" replaced by "_"
.target             target for links
.linenr             the line number in a code example
.code-highlighted   for highlighting referenced code lines
div.outline-N       div for outline level N (headline plus text))
div.outline-text-N  extra div for text at outline level N
.section-number-N   section number in headlines, different for each level
div.figure          how to format an inlined image
pre.src             formatted source code
pre.example         normal example
p.verse             verse paragraph
div.footnotes       footnote section headline
p.footnote          footnote definition paragraph, containing a footnote
.footref            a footnote reference number (always a <sup>)
.footnum            footnote number in footnote definition (always <sup>)

单独设置

  1. 下载官方的 worg.css 文件, 改名为 myorg.css,便于将来使用不同的css
  2. 在每个子目录下,通过调用
    junction css abspath/worg.css
    

    创建 css 符号连接,这样便于导出时可以方便的将css也一并打包

  3. 在每个org文件中加上
    #+STYLE: <link rel="stylesheet" type="text/css" href="css/myorg.css" />
    
  4. 在 org-publish-project-alist 变量设置中加上
    :style    "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/myorg.css\"/>"
    

    如此可保证保证生成的站点地图 index.html有 worg.css风格,如果不想要,也可以去掉

变量设置

可调用 org-insert-export-options-template时插入缺省模板

  1. 作者: user-full-name
  2. email: user-mail-address
  3. org-export-default-language zh-CN
  4. org-export-with-sub-superscripts nil ^_不转换成上/下角标

发布

目录结构

root
----org
--------.git/
--------css/
--------img/
--------share/    //可以公开发布的目录
--------work/     //工作文档,非公开目录
--------xxxx/     //其他目录
--------.gitignore //过滤掉生成的html文件

----dropbox        //dropbox同步
--------orgpublic
------------/share       //推送到github
----------------.git/
------------/work
------------/xxxx

org-publish-project-alist 设置

(setq org-publish-project-alist
  '(
    ("note-org-share"
     :base-directory "e:/gitroot/doc/org/share"
     :base-extension "org"
     :publishing-directory "e:/gitroot/doc/Dropbox/orgpublic/share"
     :recursive t
     :publishing-function org-publish-org-to-html
     :headline-levels 4
     :section-numbers t
     :auto-preamble t
     :auto-sitemap t                ; Generate sitemap.org automagically...
     :author "user"
     :email "email"
     :sitemap-filename "index.org"  ; ... call it sitemap.org (it's the default)...
     :sitemap-title "chenfan org share export index"         ; ... with title 'Sitemap'.
     :style    "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/myorg.css\"/>"
     )
    ("note-static-share"
     :base-directory "e:/gitroot/doc/org"
     :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf"
     :publishing-directory  "e:/gitroot/doc/Dropbox/orgpublic/share"
     :recursive t
     :publishing-function org-publish-attachment
     )
    ("note-org"
     :base-directory "e:/gitroot/doc/org/"
     :base-extension "org"
     :publishing-directory "e:/gitroot/doc/Dropbox/orgpublic/"
     :recursive t
     :publishing-function org-publish-org-to-html
     :headline-levels 4
     :section-numbers t
     :auto-preamble t
     :auto-sitemap t                ; Generate sitemap.org automagically...
     :author "user"
     :email "email"
     :sitemap-filename "index.org"  ; ... call it sitemap.org (it's the default)...
     :sitemap-title "chenfan org spiritual export index"         ; ... with title 'Sitemap'.
     :style    "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/myorg.css\"/>"
     )
    ("note-static"
     :base-directory "e:/gitroot/doc/org"
     :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf"
     :publishing-directory  "e:/gitroot/doc/Dropbox/orgpublic"
     :recursive t
     :publishing-function org-publish-attachment
     )
    ("note"
     :components ("note-org-share"  "note-static-share" "note-org" "note-static" ))
    ;;
    ("share"
     :components ("note-org-share"  "note-static-share"))
    ;;
    ))

存在问题: 为了自动生成share目录下的index.html, note-org-share 重复导出.

cnblogs发布

  • 配置
    1. 修改包冲突 我的emacs采用elpa管理第三方包, 当前已经包含了org2blog,xml-rpc包, 故此在采用Open_Source的插件时将metaweblog.el改名为cnmetaweblog.el 同时去掉xml-rpc.el,
    2. 加载包: 新建init-cnblogs.el,并加入到init.el中,内容如下:
      (require 'cnblogs)
      (add-hook 'org-mode-hook (lambda () (cnblogs-minor-mode)))
      (provide 'init-cnblogs)
      
    3. 配置根路径 运行cnblogs-setup-blog后输入id,用户名,密码,此时在 init.el中会明文记录设置的自定义变量,其中增加对.cnblogs根路径的设置
      (custom-set-variables
       '(cnblogs-blog-id "username")
       '(cnblogs-server-url "http://www.cnblogs.com/username/services/metaweblog.aspx")
       '(cnblogs-user-name "username")
       '(cnblogs-user-passwd "xxxxxx")
       '(cnblogs-file-root-path "e:/gitroot/doc/username/cnblogs/")
       '(session-use-package t nil (session)))
      

      在实际使用中发现设置 cnblogs-file-root-path 不一定都管用, 比如 cnblogs-get-recent-posts. 可能还有不完善的地方吧, 所以还是使用缺省值 ~/.Cnblogs

  • 快捷键
    blog发布快捷键
    快捷键命令功能备注
    C-c c pcnblogs-new-post发布博文常用
    C-c c ecnblogs-edit-post更新博文常用
    C-c c dcnblogs-delete-post删除博文常用
    C-c c ccnblogs-get-categories将分类获取到本地目前没什么实际用处
    C-c c gcnblogs-get-post获取指定ID的博文估计用户不会用到
    C-c c rcnblogs-get-recent-posts获取最近N篇博文一般只在配置扩展时使用
    C-c c scnblogs-save-draft将草稿保存到服务器不知道有什么实际用途
    C-c c ucnblogs-get-users-blogs获取用户信息对用户没什么用处
  • 注意事项
    1. 图片自动上传:: 博文中包含的本地图片自动上传,并在发布时自动替换地址
    2. 标签:: 标签写在 KEYWORDS 后面,多个标签之间用","分开。
    3. 分类:: 分类写在 CATEGORIES 后面,多个分类之间用空格隔开
    4. 摘要:: DESCRIPTION 如果为空的话,按博客园的默认设置,会将正文的一部分作为摘要
    5. 如果org文件更改了路径或者重命名,为了保持blog和org文件的绑定,需要手动修改~/.Cnblog目录下的 entry-list-file 文件. 例如:
      ((2 "vs2012安装使用" 2841160 ("[随笔分类]vs2012") "e:/gitroot/doc/org/share/vs2012/vs2012.org" "PUBLISHED") (1 "测试cnblog分类" 2841027 ("[随笔分类]emacs") "e:/gitroot/doc/org/test/test.org" "PUBLISHED"))
      

小技巧

  1. M-tab 在window平台下用:: C-M-i 替代
  2. C-c C-e t : org-insert-export-options-template
  3. org-public通过读取 .org-timestamps 目录下的 .cache 文件比较时间戳,确保只发布更新的内容,如果要重新生成,删除 该目录下的所有文件
  4. 生成utf-8编码,每个org文件都采用utf-8编码,文件头设置
    # -*- coding:utf-8 -*-
    
  5. easy temple 输入 >s 然后 按 tab
    easy temple
    s#+BEGIN_SRC … #+END_SRC
    s#+BEGIN_SRC … #+END_SRC
    q#+BEGIN_QUOTE … #+END_QUOTE
    v#+BEGIN_VERSE … #+END_VERSE
    c#+BEGIN_CENTER … #+END_CENTER
    l#+BEGIN_LaTeX … #+END_LaTeX
    L#+LaTeX:
    h#+BEGIN_HTML … #+END_HTML
    H#+HTML:
    a#+BEGIN_ASCII … #+END_ASCII
    A#+ASCII:
    i#+INDEX: line
    I#+INCLUDE: line
    可以通过 org-structure-template-alist 设置