首页 > 代码库 > 使用org-mode写cnblogs博客

使用org-mode写cnblogs博客

<style type="text/css">.title { text-align: center; margin-bottom: .2em } .subtitle { text-align: center; font-size: medium; font-weight: bold; margin-top: 0 } .todo { font-family: monospace; color: red } .done { font-family: monospace; color: green } .priority { font-family: monospace; color: orange } .tag { background-color: #eee; font-family: monospace; padding: 2px; font-size: 80%; font-weight: normal } .timestamp { color: #bebebe } .timestamp-kwd { color: #5f9ea0 } .org-right { margin-left: auto; margin-right: 0px; text-align: right } .org-left { margin-left: 0px; margin-right: auto; text-align: left } .org-center { margin-left: auto; margin-right: auto; text-align: center } .underline { text-decoration: underline } #postamble p,#preamble p { font-size: 90%; margin: .2em } p.verse { margin-left: 3% } pre { border: 1px solid #ccc; padding: 8pt; font-family: monospace; overflow: auto; margin: 1.2em } pre.src { position: relative; overflow: visible; padding-top: 1.2em } pre.src::before { display: none; position: absolute; background-color: white; top: -10px; right: 10px; padding: 3px; border: 1px solid black } pre.src:hover::before { display: inline } pre.src-asymptote::before { content: "Asymptote" } pre.src-awk::before { content: "Awk" } pre.src-C::before { content: "C" } pre.src-clojure::before { content: "Clojure" } pre.src-css::before { content: "CSS" } pre.src-D::before { content: "D" } pre.src-ditaa::before { content: "ditaa" } pre.src-dot::before { content: "Graphviz" } pre.src-calc::before { content: "Emacs Calc" } pre.src-emacs-lisp::before { content: "Emacs Lisp" } pre.src-fortran::before { content: "Fortran" } pre.src-gnuplot::before { content: "gnuplot" } pre.src-haskell::before { content: "Haskell" } pre.src-java::before { content: "Java" } pre.src-js::before { content: "Javascript" } pre.src-latex::before { content: "LaTeX" } pre.src-ledger::before { content: "Ledger" } pre.src-lisp::before { content: "Lisp" } pre.src-lilypond::before { content: "Lilypond" } pre.src-lua::before { content: "Lua" } pre.src-matlab::before { content: "MATLAB" } pre.src-mscgen::before { content: "Mscgen" } pre.src-ocaml::before { content: "Objective Caml" } pre.src-octave::before { content: "Octave" } pre.src-org::before { content: "Org mode" } pre.src-oz::before { content: "OZ" } pre.src-plantuml::before { content: "Plantuml" } pre.src-processing::before { content: "Processing.js" } pre.src-python::before { content: "Python" } pre.src-R::before { content: "R" } pre.src-ruby::before { content: "Ruby" } pre.src-sass::before { content: "Sass" } pre.src-scheme::before { content: "Scheme" } pre.src-screen::before { content: "Gnu Screen" } pre.src-sed::before { content: "Sed" } pre.src-sh::before { content: "shell" } pre.src-sql::before { content: "SQL" } pre.src-sqlite::before { content: "SQLite" } pre.src-forth::before { content: "Forth" } pre.src-io::before { content: "IO" } pre.src-J::before { content: "J" } pre.src-makefile::before { content: "Makefile" } pre.src-maxima::before { content: "Maxima" } pre.src-perl::before { content: "Perl" } pre.src-picolisp::before { content: "Pico Lisp" } pre.src-scala::before { content: "Scala" } pre.src-shell::before { content: "Shell Script" } pre.src-ebnf2ps::before { content: "ebfn2ps" } pre.src-cpp::before { content: "C++" } pre.src-abc::before { content: "ABC" } pre.src-coq::before { content: "Coq" } pre.src-groovy::before { content: "Groovy" } pre.src-bash::before { content: "bash" } pre.src-csh::before { content: "csh" } pre.src-ash::before { content: "ash" } pre.src-dash::before { content: "dash" } pre.src-ksh::before { content: "ksh" } pre.src-mksh::before { content: "mksh" } pre.src-posh::before { content: "posh" } pre.src-ada::before { content: "Ada" } pre.src-asm::before { content: "Assembler" } pre.src-caml::before { content: "Caml" } pre.src-delphi::before { content: "Delphi" } pre.src-html::before { content: "HTML" } pre.src-idl::before { content: "IDL" } pre.src-mercury::before { content: "Mercury" } pre.src-metapost::before { content: "MetaPost" } pre.src-modula-2::before { content: "Modula-2" } pre.src-pascal::before { content: "Pascal" } pre.src-ps::before { content: "PostScript" } pre.src-prolog::before { content: "Prolog" } pre.src-simula::before { content: "Simula" } pre.src-tcl::before { content: "tcl" } pre.src-tex::before { content: "TeX" } pre.src-plain-tex::before { content: "Plain TeX" } pre.src-verilog::before { content: "Verilog" } pre.src-vhdl::before { content: "VHDL" } pre.src-xml::before { content: "XML" } pre.src-nxml::before { content: "XML" } pre.src-conf::before { content: "Configuration File" } table { border-collapse: collapse } caption.t-above { caption-side: top } caption.t-bottom { caption-side: bottom } td,th { vertical-align: top } th.org-right { text-align: center } th.org-left { text-align: center } th.org-center { text-align: center } td.org-right { text-align: right } td.org-left { text-align: left } td.org-center { text-align: center } dt { font-weight: bold } .footpara { display: inline } .footdef { margin-bottom: 1em } .figure { padding: 1em } .figure p { text-align: center } .inlinetask { padding: 10px; border: 2px solid gray; margin: 10px; background: #ffffcc } #org-div-home-and-up { text-align: right; font-size: 70%; white-space: nowrap } textarea { } .linenr { font-size: smaller } .code-highlighted { background-color: #ffff00 } .org-info-js_info-navigation { border-style: none } #org-info-js_console-label { font-size: 10px; font-weight: bold; white-space: nowrap } .org-info-js_search-highlight { background-color: #ffff00; color: #000000; font-weight: bold } .org-svg { width: 90% }</style>

使用org-mode写cnblogs博客

Table of Contents

  • 1. 下载
  • 2. 配置
  • 3. 使用org写文章
  • 4. 发布

1 下载

https://github.com/yangwen0228/unimacs/tree/master/packages/vendor/cnblogs

2 配置

设置cnblogs的url和username,然后,Alt+x运行cnblogs-setup-blog,依次输入博客的名称(一般和用户名相同,比如我的是yangwen0228),用户名和密码就可以了。

(use-package cnblogs
    :ensure nil
    :init
    (requirecnblogs)
    (cnblogs-minor-mode t)
    ;; Run command: cnblogs-setup-blog to set up.
    (bind-keys ("C-c c p" . cnblogs-post)
               ("C-c c n" . cnblogs-new-post)
               ("C-c c e" . cnblogs-edit-post)
               ("C-c c d" . cnblogs-delete-post))
    ;; Bugfix: error url-http-create-request: Multibyte text in HTTP request
    ;; @ http://www.cnblogs.com/yangwen0228/p/6238528.html
    (defun url-http-create-request (&optional ref-url)
      "Create an HTTP request for `url-http-target-url‘, referred to by REF-URL."
      (let* ((extra-headers)
             (request nil)
             (no-cache (cdr-safe (assoc "Pragma" url-http-extra-headers)))
             (using-proxy url-http-proxy)
             (proxy-auth (if (or (cdr-safe (assoc "Proxy-Authorization"
                                                  url-http-extra-headers))
                                 (not using-proxy))
                             nil
                           (let ((url-basic-auth-storage
                                  ‘url-http-proxy-basic-auth-storage))
                             (url-get-authentication url-http-proxy nil ‘any nil))))
             (real-fname (url-filename url-http-target-url))
             (host (url-http--encode-string (url-host url-http-target-url)))
             (auth (if (cdr-safe (assoc "Authorization" url-http-extra-headers))
                       nil
                     (url-get-authentication (or
                                              (and (boundp ‘proxy-info)
                                                   proxy-info)
                                              url-http-target-url) nil ‘any nil))))
        (if (equal "" real-fname)
            (setq real-fname "/"))
        (setq no-cache (and no-cache (string-match "no-cache" no-cache)))
        (if auth
            (setq auth (concat "Authorization: " auth "\r\n")))
        (if proxy-auth
            (setq proxy-auth (concat "Proxy-Authorization: " proxy-auth "\r\n")))

        ;; Protection against stupid values in the referrer
        (if (and ref-url (stringp ref-url) (or (string= ref-url "file:nil")
                                               (string= ref-url "")))
            (setq ref-url nil))

        ;; We do not want to expose the referrer if the user is paranoid.
        (if (or (memq url-privacy-level ‘(low high paranoid))
                (and (listp url-privacy-level)
                     (memq ‘lastloc url-privacy-level)))
            (setq ref-url nil))

        ;; url-http-extra-headers contains an assoc-list of
        ;; header/value pairs that we need to put into the request.
        (setq extra-headers (mapconcat
                             (lambda (x)
                               (concat (car x) ": " (cdr x)))
                             url-http-extra-headers "\r\n"))
        (if (not (equal extra-headers ""))
            (setq extra-headers (concat extra-headers "\r\n")))

        ;; This was done with a call to `format‘.  Concatenating parts has
        ;; the advantage of keeping the parts of each header together and
        ;; allows us to elide null lines directly, at the cost of making
        ;; the layout less clear.
        (setq request
              (concat
               ;; The request
               (or url-http-method "GET") " "
               (url-http--encode-string
                (if using-proxy (url-recreate-url url-http-target-url) real-fname))
               " HTTP/" url-http-version "\r\n"
               ;; Version of MIME we speak
               "MIME-Version: 1.0\r\n"
               ;; (maybe) Try to keep the connection open
               "Connection: " (if (or using-proxy
                                      (not url-http-attempt-keepalives))
                                  "close" "keep-alive") "\r\n"
                                  ;; HTTP extensions we support
                                  (if url-extensions-header
                                      (format
                                       "Extension: %s\r\n" url-extensions-header))
                                  ;; Who we want to talk to
                                  (if (/= (url-port url-http-target-url)
                                          (url-scheme-get-property
                                           (url-type url-http-target-url) ‘default-port))
                                      (format
                                       "Host: %s:%d\r\n" (puny-encode-domain host)
                                       (url-port url-http-target-url))
                                    (format "Host: %s\r\n" (puny-encode-domain host)))
                                  ;; Who its from
                                  (if url-personal-mail-address
                                      (concat
                                       "From: " url-personal-mail-address "\r\n"))
                                  ;; Encodings we understand
                                  (if (or url-mime-encoding-string
                                          ;; MS-Windows loads zlib dynamically, so recheck
                                          ;; in case they made it available since
                                          ;; initialization in url-vars.el.
                                          (and (eq ‘system-type ‘windows-nt)
                                               (fboundp ‘zlib-available-p)
                                               (zlib-available-p)
                                               (setq url-mime-encoding-string "gzip")))
                                      (concat
                                       "Accept-encoding: " url-mime-encoding-string "\r\n"))
                                  (if url-mime-charset-string
                                      (concat
                                       "Accept-charset: "
                                       (url-http--encode-string url-mime-charset-string)
                                       "\r\n"))
                                  ;; Languages we understand
                                  (if url-mime-language-string
                                      (concat
                                       "Accept-language: " url-mime-language-string "\r\n"))
                                  ;; Types we understand
                                  "Accept: " (or url-mime-accept-string "*/*") "\r\n"
                                  ;; User agent
                                  (url-http-user-agent-string)
                                  ;; Proxy Authorization
                                  proxy-auth
                                  ;; Authorization
                                  auth
                                  ;; Cookies
                                  (when (url-use-cookies url-http-target-url)
                                    (url-http--encode-string
                                     (url-cookie-generate-header-lines
                                      host real-fname
                                      (equal "https" (url-type url-http-target-url)))))
                                  ;; If-modified-since
                                  (if (and (not no-cache)
                                           (member url-http-method ‘("GET" nil)))
                                      (let ((tm (url-is-cached url-http-target-url)))
                                        (if tm
                                            (concat "If-modified-since: "
                                                    (url-get-normalized-date tm) "\r\n"))))
                                  ;; Whence we came
                                  (if ref-url (concat
                                               "Referer: " ref-url "\r\n"))
                                  extra-headers
                                  ;; Length of data
                                  (if url-http-data
                                      (concat
                                       "Content-length: " (number-to-string
                                                           (length url-http-data))
                                       "\r\n"))
                                  ;; End request
                                  "\r\n"
                                  ;; Any data
                                  url-http-data))
        ;; Bug#23750
        (setq request (url-http--encode-string request))
        (unless (= (string-bytes request)
                   (length request))
          (error "Multibyte text in HTTP request: %s" request))
        (url-http-debug "Request is: \n%s" request)
        request))
    )

3 使用org写文章

用org写文章,org-mode可以去看org-mode的官网,或者是其他的教程介绍,一旦用上,就再也离不开了。 其中有几个常用的tags:

  • TITLE
  • CATEGORIES
  • DESCRIPTION
  • KEYWORDS
  • DATE

因为这是每次写cnblogs都需要的,所以,我做了一个snippet:

#+TITLE: $1
#+CATEGORIES: ${2:Emacs}
#+DESCRIPTION: ${3:}
#+KEYWORDS: $4
#+DATE: `(format-time-string "%Y-%m-%d %H:%M")`

这样,每次写博客之前,只需要输入cnblogs,然后tab一下,就可以把这个插入到文章的开头了,然后根据实际需要再填入相关的信息。

4 发布

只需要按 C-c c p 进行发布就可以了,如果是第一次发布,会提示"Do you want to post this blog to cnblogs?" ,回答"y"。如果是更新之前发表过的文章,"Already published! Do you want to update this blog to cnblogs?",回答"y"就可以更新文章。

Date: 2016-12-30 23:47

Created: 2016-12-31 周六 08:20

Validate

使用org-mode写cnblogs博客