[心得]用lisp生成网页导航、索引
am 07.09.2012 09:57:15 von 觉知的机器[b]1 缘起[/b]
用org-mode有日子了,写了些笔记,零星生成了网页,最近学习elisp,用学到的知识,做 了网页的自动生成索引,为代码显示加上漂亮的图片框,自己挺满意,总结一下:),请看生的index界面。
首先,在~/org/journal/中写笔记,输出目录为~/org_html,在.emacs中加上:
1: (setq org-publish-project-alist
2: '(("myorg"
3: :base-directory "~/org/journal/"
4: :publishing-directory "~/org_html/"
5: :section-numbers nil
6: :table-of-contents nil
7: :style "8: href=\"css/core.css\"
9: type=\"text/css\">")))
[b]2 添加导航
2.1 要求[/b]
用org-export生成的html中没有导航,要给�案鑫募�添加导航是一件无聊的事,我想 让emacs自动扫描目录,找到html文件,自动加上导航
[b]2.2 实现方法[/b]
用org-export生成的html文件都有如下部分:
.. 省略 ...
machine index
.. 省略 ...
在指定位置添加如下一段:
计划这样实现要求:
* 打开文件
* 如果没有找到 ,就需要添加导航
* 找到
* 用html-mode的sgml-skip-tag-backward函数将光标移到|
* 插入导航的代码段
[b]2.3 添加导航[/b]
1: (setq changedItems '())
2:
3: (defun machine_make_html_tranlate (file)
4: "tranlate html文件.
5: 1. 增加导航
6: 2. 给代码添加图片框"
7: (let (name buffer text star end)
8: (find-file file)
9: (setq name (file-name-nondirectory file))
10: (setq buffer (get-file-buffer name))
11: (goto-char (point-min))
注意最后一句,(setq changedItems (cons …,用于记录添加导航操 作,changedItems的定义在第一行。
待续...
--
[upload=1][/upload]
[36m※ 修改:・awareness 于 Sep 7 17:57:14 2012 修改本文・[m
[m[33m※ 来源:・水木社区 http://newsmth.net・[FROM: 118.181.195.*][m
Re: [心得]用lisp生成网页导航、索引
am 07.09.2012 13:51:33 von 觉知的机器[b]3 给代码添图片框
3.1 要求[/b]
曾经在 张驰原 的网页看到code周围,有像emacs环境图片,见http://lifegoo.pluskid.org/wiki/EmacsSmartCompile.html,很喜欢,也想弄一下。
因此实现方式由以下步骤组成:
* 分解code的底图,分成6个独立的小图片
* 定义6个图片的css
* 在html文件中找到"
在css中,定义底图的定义:
.emacs-title-left {
background-image: url(../images/title-left.png);
background-position: top left;
background-repeat: no-repeat;
padding: 0;
padding-top: 30px;
}
.emacs-title-right {
background-image: url(../images/title-right.png);
background-position: top right;
background-repeat: no-repeat;
padding: 0;
}
.emacs-cont-left {
background-image: url(../images/cont-left.png);
background-position: top left;
background-repeat: no-repeat;
padding: 0;
}
.emacs-cont-right {
background-image: url(../images/cont-right.png);
background-position: top right;
background-repeat: no-repeat;
padding: 0;
}
.emacs-footer-right {
background-image: url(../images/footer-right.png);
background-position: bottom right;
background-repeat: no-repeat;
padding: 0;
}
.emacs-footer-left {
background-image: url(../images/footer-left.png);
background-position: bottom left;
background-repeat: no-repeat;
padding: 0;
padding-bottom: 16px;
padding-left: 15px;
padding-right: 3px;
}
[b]3.3 添加图片框[/b]
1: ... 省略 ...
2: ;; for code add round image
3: (goto-char (point-min))
4: (while (search-forward " 5: (sgml-skip-tag-backward 1)
6: (beginning-of-line)
7: (backward-char 1)
8: (beginning-of-line)
9: (setq text (buffer-substring-no-properties (1+ (point)) (+ (point) 4)))
10: (if (not (string= "div" text))
11: (progn
12: (insert (concat "
13: "
14: "
15: "
16: "
17: " " "
21: (setq changedItems
22: (cons (concat "tranlate " (buffer-file-name) "'s code at " (number-to-string (line-number-at-pos)))
23: changedItems)))
24: (search-forward "" nil t)))
25: ... 省略 ...
* (sgml-skip-tag-backward 1),sgml-mode中,跳到tag的开始
* (beginning-of-line),光标移到行首
* (if (not (string= "div" text)),判断是否己经有图片框
* (setq changedItems (cons (concat …,添加�c改记录
待续...
--
[m[34m※ 来源:・水木社区 http://newsmth.net・[m
Re: [心得]用lisp生成网页导航、索引
am 07.09.2012 13:55:14 von 觉知的机器[b]4 根据图片大小,�c改图片html[/b]
[b]4.1 要求[/b]
org-export生成的img是这样的:
没有定义图片的��度,我需要根据图片大小添加width,同时去除图片外的。
* 如图片��度是430,那么�c改成的img
如图片太��,大于800,如图片��度1600,那么�c改成的img
[b]4.2 实现方法[/b]
* 找到html代码中的img,取出图片文件名
* 用identify -format "%w" filename,得到图片��度
* ��度大子800,用replace-match,�c改width="800"
* ��度小于800,用replace-match,�c改width="实际��度"
[b]4.3 �c改图片html[/b]
1: ;; image width
2: (goto-char (point-min))
3: (while (re-search-forward "" nil t)
4: (setq name (match-string-no-properties 1))
5: (setq string
6: (shell-command-to-string
7: (concat "identify -format \"%w\" " org_html_path name)))
8: (setq width (string-to-number string))
9: (if (> width 800)
10: (replace-match "")
11: (replace-match ""))
12: (setq changedItems
13: (cons (concat "tranlate " (buffer-file-name) "'s image at " (number-to-string (line-number-at-pos)))
14: changedItems)))
* shell-command-to-string,在elisp中调用shell命令
* (setq changedItems (cons (concat …,添加�c改记录
待续...
--
[m[34m※ 来源:・水木社区 http://newsmth.net・[m
Re: [心得]用lisp生成网页导航、索引
am 07.09.2012 13:57:10 von 觉知的机器[b]5 综合[/b]
现在,我能对一个html文件:
1. 增加导航
2. 给代码添加图片框
3. 根据图片大小,�c改图片html
org-export生成的若于html文件,都需要做处理,我想用一个函数实现,这到目的:
* 自动在org-export目标目录中找到所有html文件,放入list
* 用mapcar处理�案鑫募�
* 记录�耙桓龃�理,全部文件处理完成后,显示到临时buffer中
[b]5.1 单个文件转换函数[/b]
完成:
1. 增加导航
2. 给代码添加图片框
3. 根据图片大小,�c改图片html
1: ;; org public path
2: (setq org_html_path "~/org_html/")
3: (setq changedItems '())
4:
5: (defun machine_make_html_tranlate (file)
6: "tranlate html文件.
7: 1. 增加导航
8: 2. 给代码添加图片框
9: 3. 根据图片大小,�c改图片html"
10: (let (name buffer text star end)
11: (find-file file)
12: (setq name (file-name-nondirectory file))
13: (setq buffer (get-file-buffer name))
14: (goto-char (point-min))
15: ;; nav
16: (if (not (search-forward "
18: (search-forward "19: (sgml-skip-tag-backward 1)
20: (insert (concat "
21: "
- Menu\n"
22: " \n"
23: " \n"
24: "
25: "
26: (setq changedItems (cons (concat "add " (buffer-file-name) "'s nav.") changedItems))))
27: ;; for code add round image
28: (goto-char (point-min))
29: (while (search-forward "30: (sgml-skip-tag-backward 1)
31: (beginning-of-line)
32: (backward-char 1)
33: (beginning-of-line)
34: (setq text (buffer-substring-no-properties (1+ (point)) (+ (point) 4)))
35: (if (not (string= "div" text))
36: (progn
37: (insert (concat "
38: "
39: "
40: "
41: "
42: " " "
46: (setq changedItems
47: (cons (concat "tranlate " (buffer-file-name) "'s code at " (number-to-string (line-number-at-pos)))
48: changedItems)))
49: (search-forward "" nil t)))
50: ;; image width
51: (goto-char (point-min))
52: (while (re-search-forward "" nil t)
53: (setq name (match-string-no-properties 1))
54: (setq string
55: (shell-command-to-string
56: (concat "identify -format \"%w\" " org_html_path name)))
57: (setq width (string-to-number string))
58: (if (> width 800)
59: (replace-match "
60: (replace-match "
61: (setq changedItems
62: (cons (concat "tranlate " (buffer-file-name) "'s image at " (number-to-string (line-number-at-pos)))
63: changedItems)))
64: (if (not (buffer-modified-p))
65: (kill-this-buffer)
66: (progn
67: (save-buffer)))))
* (if (not (buffer-modified-p)),判断buffer是否己�c改
* 未�c改,kill-this-buffer
* 己�c改,save-buffer,并保持打开
[b]5.2 处理�案鑫募�[/b]
1: (defun org-html-tranlate ()
2: "转�Qorg public path下的所有html.
3: 在文件中输出log."
4: (interactive)
5: (setq changedItems '())
6: (setq outputBuffer "*machine org html tranlate output*")
7: (with-output-to-temp-buffer outputBuffer
8: (mapcar 'machine_make_html_tranlate
9: (find-lisp-find-files org_html_path "\\.html$"))
10: (mapcar 'print (reverse changedItems))
11: (princ "\n\n--- Done deal! ---")))
* 用(find-lisp-find-files org_html_path "\\.html$")),在org_html_path目录下寻 找html文件
* (mapcar 'machine_make_html_tranlate …,对�案鑫募�调用 machine_make_html_tranlate
* (with-output-to-temp-buffer outputBuffer …,在outputBuffer中显示�c改记录
* (reverse changedItems),将 changedItems倒序
待续...
--
[m[34m※ 来源:・水木社区 http://newsmth.net・[m
Re: [心得]用lisp生成网页导航、索引
am 07.09.2012 14:01:01 von 觉知的机器[b]6 生成index.html[/b]
[b]6.1 要求[/b]
我有很多笔记,分成emacs、linux、latex、work等类,�霸黾右桓觯�在索引文件 (index.html)中都要�c改,很烦;我想让自动收集目录下的文件,并分类,生成 index.html,多方便。
[b]6.2 实现方法[/b]
先建立index.html框架,将需要的类别写好:
... 省略 ...
emacs
latex
linux
c/c++
java
work
other
... 省略 ...
* 在dired-mode中,选中若于文件
* 给定类别名,在index.html中,找到类别所在位置
* 依次打开文件,找到文件title,用title作为文件的链接名
* 更新index.html的日期
[b]6.3 生成index[/b]
1: (defun org-make-html-index (type)
2: "get the 特定类型的 html,在 index.html 中分类."
3: (interactive "stype: ")
4: (let (text name title filename list start)
5: (setq list (dired-get-marked-files))
6: (setq name "index.html")
7: (setq filename (concat org_html_path name))
8: (if (not (file-exists-p filename))
9: (error "no %s" name))
10: (find-file filename)
11: (goto-char (point-min))
12: (setq text nil)
13: (while (not (string= text type))
14: (if (not (search-forward "
" nil t))
15: (error "no %s type" type))
16: (setq start (point))
17: (search-forward "
")
18: (backward-char 5)
19: (setq text (buffer-substring-no-properties start (point))))
20: ;; find type
21: (search-forward "
22: (forward-char 1)
23: (setq start (point))
24: (search-forward "25: (beginning-of-line)
26: (delete-region start (point))
27: (mapcar
28: (lambda (name)
29: (find-file name)
30: (goto-char (point-min))
31: (search-forward "
32: (setq start (point))
33: (search-forward "
34: (backward-char 8)
35: (setq title (buffer-substring-no-properties start (point)))
36: (bury-buffer)
37: (insert (concat "\n" "
38: ""
39: title "\n" "
40: list)
41: (insert "\n")
42: ;; update date
43: (search-forward "
Date: " nil t)
44: (setq start (point))
45: (search-forward "
46: (backward-char 4)
47: (delete-region start (point))
48: (insert (format-time-string "%Y-%m-%d %H:%M:%S"))
49: (save-buffer)))
* 这函数要在dired-mode中运行
* (interactive "stype: "),提示输入类型,"s"表示输入字符串,其他输入方式参 见:(info)interactive
* (setq list (dired-get-marked-files)),取出marked files
* 如里在index.html中找不到type,退出
.. 省略 ...
(while (not (string= text type))
(if (not (search-forward "