github-markdown-server 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d0fb00cc94750513ce5f5fc8e415f7701451438e
4
- data.tar.gz: 1716202620cb37f2521f38a4ba575014a62ccf6f
3
+ metadata.gz: 69be47f30baf11233fbbf406bbaedd1c3cf38e50
4
+ data.tar.gz: 3c52501a7afc9631bce30d01937da20fe7fa14d8
5
5
  SHA512:
6
- metadata.gz: 83862dda94532d5f09a36e136106067a812595339b56b64d4c6fd2e34d643f3978c4beb92d0a934214a98c18c6fa8b090af42aeccd354112adce26e79d16735d
7
- data.tar.gz: 77a06eb4a0140a06643466dc047f27d4bf935744e52698aa8873ba056318002e8569f8ea114662e2a7e996014388faa66e47640d7e57cbfd2386cc604bdc380a
6
+ metadata.gz: dff3dd5ab3f042f055595ec68496b885c2f8e3da8287942f943d34c8a4a96c008034954d9becb15afda2bae06021903883c322e670f03453a8170a961f21c89e
7
+ data.tar.gz: 8a6834b4856f7f0cea24d85daf4ed038db6455138c2f4fea16db755e074fce259e8130fc7d0dd2fa583a8a8558ad4243c180ccdf3f18ceb48d0707e4668b9eb1
data/README.md CHANGED
@@ -10,13 +10,13 @@ github-markdown-server is built on top of [github-markdown-preview](https://gith
10
10
 
11
11
  ## Installation
12
12
 
13
- ```shell
13
+ ```bash
14
14
  gem install github-markdown-server
15
15
  ```
16
16
 
17
17
  ## Usage
18
18
 
19
- ```shell
19
+ ```bash
20
20
  github-markdown-server README.md
21
21
  ```
22
22
 
@@ -24,7 +24,7 @@ This will start a server serving in the current directory and all child director
24
24
 
25
25
  If you are deep down inside a git repository and you want to serve the whole repository (so that relative links will work) then this command will do that for you.
26
26
 
27
- ```shell
27
+ ```bash
28
28
  github-markdown-server -d $(git rev-parse --show-toplevel) somefile.md
29
29
  ```
30
30
 
@@ -35,3 +35,15 @@ There is a [contrib](contrib/) directory with an emacs lisp file which will star
35
35
  ## Contributing
36
36
 
37
37
  Please feel free to send me pull requests! This is my first Ruby project and it always feels like my first time when I write Emacs Lisp.
38
+
39
+ ## Building
40
+
41
+ I keep forgetting how to do this...
42
+
43
+ ```bash
44
+ VERSION=$(./bin/github-markdown-server --version | cut -d ' ' -f 3)
45
+ \cp -fv ~/Library/emacs/lisp/github-markdown-server.el ~/mysrc/github-markdown-server/contrib/github-markdown-server.el
46
+ gem build github-markdown-server.gemspec
47
+ gem install github-markdown-server-$VERSION.gem
48
+ gem push github-markdown-server-$VERSION.gem
49
+ ```
@@ -3,6 +3,12 @@
3
3
 
4
4
  ;;; Commentary:
5
5
 
6
+ ;;; view-file-in-browser will try and show the current file in the best way in the current browser.
7
+
8
+ ;;; Since I use rvm you'll need rvm.el from here https://github.com/senny/rvm.el
9
+ ;;; Uncommenthing this _might_ work (untested)
10
+ ;;; (defun rvm-use-default () ".") (provide 'rvm)
11
+
6
12
  ;;; Code:
7
13
 
8
14
  (require 'cl-lib)
@@ -10,25 +16,48 @@
10
16
  (defvar github-markdown-servers '() "The servers we have running.")
11
17
  (defvar github-markdown-server-port 7494 "The next available server port.")
12
18
 
19
+ ;; from the emacs wiki cookbook
20
+ (defun ark-chomp (str)
21
+ "Chomp leading and trailing whitespace from STR."
22
+ (replace-regexp-in-string (rx (or (: bos (* (any " \t\n"))) (: (* (any " \t\n")) eos))) "" str))
23
+
13
24
  (defun git-base (&optional file-name)
14
25
  "Get the base of a git repository for FILE-NAME (or the current buffer)."
15
- (interactive (buffer-file-name))
16
- (shell-command-to-string
17
- (concat "git -C " (shell-quote-argument (directory-file-name file-name))
18
- " rev-parse --show-toplevel 2>/dev/null")))
26
+ (if (not file-name) (setq file-name (buffer-file-name)))
27
+ (let* ((dirname (file-name-directory file-name))
28
+ (cmd (concat "git -C " (shell-quote-argument dirname) " rev-parse --show-toplevel 2>/dev/null")))
29
+ (ark-chomp (shell-command-to-string cmd))))
30
+
31
+ (defun github-markdown-server-start (directory port file-name)
32
+ "Start a github-markdown-server for DIRECTORY on PORT and open FILE-NAME."
33
+ (let ((cmd (concat
34
+ "github-markdown-server"
35
+ " --directory=" (shell-quote-argument directory)
36
+ " --port=" (number-to-string port)
37
+ " " (shell-quote-argument file-name))))
38
+ (call-process-shell-command cmd nil 0)))
19
39
 
20
40
  (defun github-markdown-serve (&optional file-name)
21
41
  "Get the base of a git repository for FILE-NAME (or the current buffer)."
22
42
  (interactive (list (buffer-file-name)))
43
+ (ark-reuse-or-start-server file-name 'github-markdown-server-start)
44
+ )
45
+
46
+ (defun ark-reuse-or-start-server (file-name start-command &optional path-name-fixer)
47
+ "Reuse a server if we can, otherwise start a new server.
48
+ FILE-NAME what we are serving.
49
+ START-COMMAND the command to start a server."
50
+ (if (not path-name-fixer) (setq path-name-fixer (lambda (file-name path-name) path-name)))
23
51
  (let ((base (git-base file-name)) (server nil) (server-running nil))
24
52
  ;;;
25
53
  (progn
26
- (if (< (length base) 1) (setq base (directory-file-name file-name)))
54
+ (if (< (length base) 1) (setq base (file-name-directory file-name)))
27
55
  ;; find a server that might still be running
28
- (setq server (cl-reduce (lambda (a b)
29
- (if (and (string= (car b) (substring base 0 (length (car b))))
30
- (or (not (car a)) (< (length (car b)) (length (car a)))))
31
- b a)) (cons nil github-markdown-servers)))
56
+ (setq server
57
+ (cl-reduce (lambda (a b)
58
+ (if (and (string= (car b) (substring base 0 (min (length base) (length (car b)))))
59
+ (or (not (car a)) (< (length (car b)) (length (car a)))))
60
+ b a)) (cons nil github-markdown-servers)))
32
61
  (if (and server
33
62
  (progn
34
63
  (message (concat "Checking if server running at" (prin1-to-string server)))
@@ -43,58 +72,112 @@
43
72
  (shell-quote-argument
44
73
  (concat "http://localhost:"
45
74
  (number-to-string (cdr server))
46
- (substring file-name (length (car server))))))))
75
+ (funcall path-name-fixer file-name (substring file-name (length (car server)))))))))
47
76
  (progn
48
77
  (if server
49
78
  (message (concat "re-starting github-markdown-server on port:" (number-to-string (cdr server))))
50
79
  (progn
51
80
  ;; find the next available port
52
81
  (while (< 0 (length (shell-command-to-string
53
- (concat "lsof -n -i4TCP:" (number-to-string github-markdown-server-port) " | grep LISTEN"))))
82
+ (concat "lsof -n -i4TCP:" (number-to-string github-markdown-server-port) " | grep LISTEN"))))
54
83
  (setq github-markdown-server-port (+ github-markdown-server-port 1)))
55
84
 
56
- (setq server (cons base github-markdown-server-port)))
57
- (message (concat "starting github-markdown-server on port:" (number-to-string (cdr server)))))
58
- (call-process-shell-command
59
- (concat "github-markdown-server"
60
- " --directory=" (shell-quote-argument (car server))
61
- " --port=" (number-to-string (cdr server))
62
- " " (shell-quote-argument file-name))
63
- nil 0)
64
- (if (not server)
65
- (progn
85
+ (setq server (cons base github-markdown-server-port))
86
+ (message (concat "starting server on port:" (number-to-string (cdr server))))
66
87
  (add-to-list 'github-markdown-servers server)
67
- (setq github-markdown-server-port (+ github-markdown-server-port 1))))))
88
+ (setq github-markdown-server-port (+ github-markdown-server-port 1))))
89
+ (funcall start-command (car server) (cdr server) file-name)))
68
90
  (message ""))))
69
91
 
92
+ (defun jekyll-get-permalink-from-file (file-name path-name)
93
+ "Get a permalink from FILE-NAME or work it out from suggest PATH-NAME."
94
+ ;;(read-string (concat file-name " --- " path-name))
95
+ (if (not (string= path-name "/"))
96
+ (with-temp-buffer
97
+ (insert-file-contents file-name)
98
+ (goto-char (point-min))
99
+ (if (= (search-forward-regexp "^---" 6 't) 4)
100
+ (progn
101
+ (goto-char (+ (point-min) 3))
102
+ (if (search-forward-regexp "^---" (point-max) 't)
103
+ (let ((bound (point)))
104
+ (goto-char (point-min))
105
+ (if (search-forward-regexp "^permalink: \\(.*\\)$" bound 't)
106
+ (setq path-name (match-string 1)))))))))
107
+ path-name)
108
+
109
+ (defun jekyll-fix-path-name (file-name path-name)
110
+ "Work out the pathname that FILE-NAME will be served at (PATH-NAME is our best guess right now)."
111
+ (setq path-name (replace-regexp-in-string "\\.md$" ".html" path-name))
112
+ (jekyll-get-permalink-from-file file-name
113
+ (cond
114
+ ((string-match "^/_posts/\\([0-9][0-9][0-9][0-9]\\)-\\([0-9][0-9]\\)-[0-9][0-9]-\\(.*\\)" path-name)
115
+ (concat "/" (match-string 1 path-name) "/" (match-string 2 path-name) "/" (match-string 3 path-name)))
116
+ ((string-match "^/[^_]" path-name) path-name)
117
+ ('t "/"))))
118
+
119
+ (defun jekyll-server-start (directory port file-name)
120
+ "Start a Jekyll server for DIRECTORY on PORT and open FILE-NAME."
121
+ (require 'rvm)
122
+ (rvm-use-default)
123
+ (let* (
124
+ (url (concat "http://localhost:" (number-to-string port)
125
+ (jekyll-fix-path-name file-name (substring file-name (length directory)))))
126
+ (cmd (concat
127
+ "cd " (shell-quote-argument directory)
128
+ ";jekyll serve -q "
129
+ " --port " (number-to-string port)
130
+ ";open " (shell-quote-argument url))))
131
+ (call-process-shell-command cmd nil 0)))
132
+
133
+ (defun jekyll-file-p (file-name)
134
+ "Is FILE-NAME part of a jekyll site."
135
+ (string-match "\.github\.io/" file-name))
136
+
137
+ (defun jekyll-serve-file (&optional file-name)
138
+ "Find or start a server to serve FILE-NAME from a jekyll site."
139
+ (interactive (buffer-file-name))
140
+ (ark-reuse-or-start-server file-name 'jekyll-server-start 'jekyll-fix-path-name)
141
+ )
142
+
143
+
70
144
  (defun view-file-in-browser (&optional file-name)
71
- "Open up file on github or via a local markdown server.
145
+ "Open up file in one of four ways.
146
+
147
+ 1) on a local Jekyll server if the path contains .github.io/ (requires rvm.el)
148
+ 2) via a local https://rubygems.org/gems/github-markdown-server
149
+ 3) on github (if file is in a github.com repository)
150
+ 4) via a file:/// url (last resort)
72
151
  FILE-NAME will be current buffer if not specified.
73
- Prefix arg \[universal-argument] to not run local server for markdown files."
152
+ Prefix arg \[universal-argument] to not run local server and open on github or via file://."
74
153
  (interactive (list (buffer-file-name)))
75
- (if (and (not current-prefix-arg) (string-match "\.md$" file-name))
76
- (github-markdown-serve file-name)
77
- (call-process-shell-command
78
- ;; open it with the default webbrowser The Mac::InternetConfig stuff)
79
- ;; open either on github or a local file (the remove.origin.url stuff)
80
- (concat "open -a \"$(VERSIONER_PERL_PREFER_32_BIT=1 "
81
- "perl -MMac::InternetConfig -le 'print +(GetICHelper \"http\")[1]')\" \"$("
82
- "perl -e 'use File::Basename; $f = shift @ARGV; $d = dirname($f); $b = basename($f); "
83
- "$o = `git -C $d config --get remote.origin.url`; "
84
- "if ($o =~ qr!git(@|://)github.com[:/]!) { "
85
- " $o =~ s!git(@|://)github.com[:/](.*).git$!https://github.com/$2!; "
86
- "} else { $o = \"\" }"
87
- "$p = `git -C $d rev-parse --show-prefix`; "
88
- "chomp($o, $p); "
89
- "if ($o) { print \"$o/blob/master/$p$b\"; } else { print \"file://$f\" }' "
90
- (shell-quote-argument (buffer-file-name)) ")#L"
91
- (if (region-active-p)
92
- (concat
93
- (number-to-string (line-number-at-pos (region-beginning))) "-"
94
- (number-to-string (line-number-at-pos (region-end))))
95
- (number-to-string (line-number-at-pos)))
154
+ (cond
155
+ ((and (not current-prefix-arg) (jekyll-file-p file-name))
156
+ (jekyll-serve-file file-name))
157
+ ((and (not current-prefix-arg) (string-match "\.md$" file-name))
158
+ (github-markdown-serve file-name))
159
+ ('t
160
+ (call-process-shell-command
161
+ ;; open it with the default webbrowser The Mac::InternetConfig stuff)
162
+ ;; open either on github or a local file (the remove.origin.url stuff)
163
+ (concat "open -a \"$(VERSIONER_PERL_PREFER_32_BIT=1 "
164
+ "perl -MMac::InternetConfig -le 'print +(GetICHelper \"http\")[1]')\" \"$("
165
+ "perl -e 'use File::Basename; $f = shift @ARGV; $d = dirname($f); $b = basename($f); "
166
+ "$o = `git -C $d config --get remote.origin.url`; "
167
+ "if ($o =~ qr!git(@|://)github.com[:/]!) { "
168
+ " $o =~ s!git(@|://)github.com[:/](.*).git$!https://github.com/$2!; "
169
+ "} else { $o = \"\" }"
170
+ "$p = `git -C $d rev-parse --show-prefix`; "
171
+ "chomp($o, $p); "
172
+ "if ($o) { print \"$o/blob/master/$p$b\"; } else { print \"file://$f\" }' "
173
+ (shell-quote-argument (buffer-file-name)) ")#L"
174
+ (if (region-active-p)
175
+ (concat
176
+ (number-to-string (line-number-at-pos (region-beginning))) "-"
177
+ (number-to-string (line-number-at-pos (region-end))))
178
+ (number-to-string (line-number-at-pos)))
96
179
  "\"")
97
- nil 0)))
180
+ nil 0))))
98
181
 
99
182
  (provide 'github-markdown-server)
100
183
  ;;; github-markdown-server ends here
@@ -1,3 +1,3 @@
1
1
  module GithubMarkdownServer
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: github-markdown-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex K (wtwf.com)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-13 00:00:00.000000000 Z
11
+ date: 2014-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: github-markdown-preview