2014-01-11 97 views
0

我編寫了一個簡單的emacs模塊,它生成我用於博客的靜態站點生成器的標準模板。重構生成文件的elisp代碼

(defun hakyll-site-location() 
    "Return the location of the Hakyll files." 
    "~/Sites/hblog/") 

(defun hakyll-new-post (title tags) 
    "Create a new Hakyll post for today with TITLE and TAGS." 
    (interactive "sTitle: \nsTags: ") 
    (let ((file-name (hakyll-post-title title))) 
    (set-buffer (get-buffer-create file-name)) 
    (markdown-mode) 
    (insert 
    (format "---\ntitle: %s\ntags: %s\ndescription: \n---\n\n" title tags)) 
    (write-file 
    (expand-file-name file-name (concat (hakyll-site-location) "posts"))) 
    (switch-to-buffer file-name))) 

(defun hakyll-new-note (title) 
    "Create a new Note with TITLE." 
    (interactive "sTitle: ") 
    (let ((file-name (hakyll-note-title title))) 
    (set-buffer (get-buffer-create file-name)) 
    (markdown-mode) 
    (insert (format "---\ntitle: %s\ndescription: \n---\n\n" title)) 
    (write-file 
    (expand-file-name file-name (concat (hakyll-site-location) "notes"))) 
    (switch-to-buffer file-name))) 

(defun hakyll-post-title (title) 
    "Return a file name based on TITLE for the post." 
    (concat 
    (format-time-string "%Y-%m-%d") 
    "-" 
    (replace-regexp-in-string " " "-" (downcase title)) 
    ".markdown")) 

(defun hakyll-note-title (title) 
    "Return a file name based on TITLE for the note." 
    (concat 
    (replace-regexp-in-string " " "-" (downcase title)) 
    ".markdown")) 

現在,這種方法可行,但它可以幹一點點,但我自己並不知道有足夠的elisp來做。

  • hakyll-new-posthakyll-new-note非常相似,可以用乾燥起來做,但我不知道如何正確的參數傳遞給任何重構功能
  • 我硬編碼hakyll-site-location。有什麼方法可以請求並將配置存儲在我的emacs dotfiles中?

歡迎任何幫助或指向文檔的指針。

+5

此問題似乎是脫離主題,因爲它是要求代碼審查。也許在Code Review(或程序員?)上更好。 – Drew

回答

2

下面是代碼。我不能保證它能正常工作,但如果它在以前工作,那麼它現在應該可以工作了。

(defvar hakyll-site-location "~/Sites/hblog/" 
    "Return the location of the Hakyll files.") 

(defun hakyll-new-post (title tags) 
    "Create a new Hakyll post for today with TITLE and TAGS." 
    (interactive "sTitle: \nsTags: ") 
    (hakyll-do-write 
    (format "%s/posts/%s-%s.markdown" 
      hakyll-site-location 
      (format-time-string "%Y-%m-%d") 
      (replace-regexp-in-string " " "-" (downcase title))) 
    (format "---\ntitle: %s\ntags: %s\ndescription: \n---\n\n" 
      title 
      tags))) 

(defun hakyll-new-note (title) 
    "Create a new Note with TITLE." 
    (interactive "sTitle: ") 
    (hakyll-do-write 
    (format "%s/notes/%s.markdown" 
      hakyll-site-location 
      (replace-regexp-in-string " " "-" (downcase title))) 
    (format "---\ntitle: %s\ndescription: \n---\n\n" title))) 

(defun hakyll-do-write (file-name str) 
    (find-file file-name) 
    (insert str) 
    (save-buffer)) 

您可以在點文件中使用(setq hakyll-site-location "~/Sites/")來設置位置。 您甚至可以將defvar更改爲defcustom並使用自定義來設置位置。

+0

謝謝你的。對於'defcustom',我也不知道。 – Abizern

+0

你已經編輯了一個錯誤:現在的筆記和帖子是雙倍加入的 –

+0

D'oh!你說得很對;我沒有在格式字符串中看到它。但是我已經再次糾正過,所以筆記不會進入'.../notes/notes/...'。 – Abizern