2014-01-11 75 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