-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslashorg.el
155 lines (116 loc) · 4.79 KB
/
slashorg.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
;;; slashorg.el --- interface for reading the frontpage of slashdot.org as a buffer mode
;; Copyright (C) 2010, 2011 Rolando Pereira
;; Author: Rolando Pereira <finalyugi@sapo.pt>
;; Keywords: slashdot org-mode w3m
;; This file is NOT part of GNU Emacs.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; Note: This is basically the contents of the README file
;; Slashorg is a program that let's you read the contents of the
;; frontpage of Slashdot.org inside a org buffer.
;; Prerequisites
;; You need to have w3m installed before you can use slashorg.
;; On a Ubuntu machine you can use the following command:
;; sudo apt-get install w3m
;; You also need to have org-mode installed. Most recent versions of
;; Emacs already have it installed, but if yours doesn't, you will
;; need to install it.
;; You can also install org-mode using your package manager. For
;; example, on a Ubuntu machine you can use the following command:
;; sudo apt-get install org-mode
;; Instalation
;; Download the slashorg.el file and put it somewhere in your
;; load-path.
;; Add the following to your .emacs file:
;; (autoload 'slashorg "slashorg" "Read the front page of Slashdot.org as a `org-mode' buffer." t)
;; To use it, type M-x slashorg
;;; Code:
(defun slashorg-get-frontpage-as-list ()
"Return the contents of the frontpage of Slashdot as a list.
The list is the parsed output (ie. it doesn't contain any HTML
tags) created by the w3m executable where every `car' contains a
single line of that output."
(split-string
(shell-command-to-string "w3m -dump -cols 20000 slashdot.org") "\n"))
(defun slashorg-extract-headline (headline)
"Remove the number at the end of the headline represented by HEADLINE.
Each headline contains a number at the end indicating how many
people have responded to that news.
This function removes that number.
Example:
Foo to release Emacs 42.0 by the end of 2038 192
Output:
Foo to release Emacs 42.0 by the end of 2038"
(car
(split-string headline " [[:digit:]]*$")))
(defun slashorg-beginning-of-news-p (list)
"Check if (`car' LIST) is the beginning of a new news item.
A news item has, in the output of w3m, the following syntax:
<title of news>
<empty-line>
Posted by <username> on <date>
from the <string> dept.
<summary>
Read the <number> comments
Which in the format returned by `slashorg-get-frontpage-as-list'
corresponds to:
'(\"<title of news>\"
\"\"
\"Posted by <username> on <date>\"
\"from the <string dept.\"
\"<summary>\"
\"Read the <number> comments\")
This function checks if (`car' LIST) is currently on the line <title of news>"
(when (and list (nth 2 list))
(string-match "Posted by " (nth 2 list))))
;; TODO: Convert this function to remove the recursion so that it is
;; no longer necessary to mess around with `max-lisp-eval-depth' and
;; `max-specpdl-size' in the function
;; `slashorg-insert-content-into-buffer'
(defun slashorg-get-info (list)
"Return a string (in `org-mode' format) with the news contained in LIST.
Each news is represented in this format:
* <Title of the News>
<summary>"
(cond ((null list)
'())
((slashorg-beginning-of-news-p list)
(let ((headline (slashorg-extract-headline (car list)))
(summary (nth 4 list)))
(concat "* " headline "\n"
" " summary "\n"
(slashorg-get-info
(cdr (cdr (cdr (cdr (cdr (cdr list))))))) ; Jump over the summary
"\n")))
(t
(concat "" (slashorg-get-info (cdr list))))))
(defun slashorg-insert-content-into-buffer ()
"Insert the content of the frontpage of Slashdot in the current buffer."
(let ((max-lisp-eval-depth 2000000)
(max-specpdl-size 20000))
(insert (slashorg-get-info (slashorg-get-frontpage-as-list)))
(fill-region (point-min) (point-max))))
;;;###autoload
(defun slashorg ()
"Read the front page of Slashdot.org as a `org-mode' buffer."
(interactive)
(switch-to-buffer "*Slashorg*")
(org-mode)
(erase-buffer)
(slashorg-insert-content-into-buffer)
(org-cycle-internal-global) ;; Change the visibility to OVERVIEW
(goto-char (point-min)))
(provide 'slashorg)
;;; slashorg.el ends here