mamemose 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/mamemose/version.rb +1 -1
  2. data/lib/mamemose.rb +36 -17
  3. metadata +2 -2
@@ -1,3 +1,3 @@
1
1
  module Mamemose
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/mamemose.rb CHANGED
@@ -42,7 +42,7 @@ class Mamemose::Server
42
42
  res['Expires'] = '0'
43
43
  if req.path =~ /^\/search/
44
44
  query = req.query
45
- path = path(query["path"])
45
+ path = fullpath(query["path"])
46
46
  q = URI.decode(query["q"])
47
47
  q = q.force_encoding('utf-8') if q.respond_to?(:force_encoding)
48
48
 
@@ -61,11 +61,11 @@ class Mamemose::Server
61
61
  end
62
62
  end
63
63
 
64
- title = "Search #{q} in #{docpath(query['path'])}"
64
+ title = "Search #{q} in #{showpath(query['path'])}"
65
65
  title = title.force_encoding('utf-8') if title.respond_to?(:force_encoding)
66
66
  body = title + "\n====\n"
67
67
  found.reject{|key, value| value == []}.sort.each do |key, value|
68
- body += "\n### in <a href='#{uri(key)}'>#{uri(key)}\n"
68
+ body += "\n### in <a href='#{uri(key)}'>#{escape(uri(key))}</a>\n"
69
69
  value.each do |v|
70
70
  body += link_list(v[0], v[1])
71
71
  end
@@ -76,10 +76,10 @@ class Mamemose::Server
76
76
 
77
77
  else
78
78
 
79
- filename = path(req.path)
79
+ filename = fullpath(req.path)
80
80
 
81
81
  if File.directory?(filename) then
82
- title = "Index of #{docpath(req.path)}"
82
+ title = "Index of #{showpath(req.path)}"
83
83
  body = title + "\n====\n"
84
84
 
85
85
  recent = []
@@ -96,7 +96,8 @@ class Mamemose::Server
96
96
  recent = recent.map{|file|
97
97
  if markdown?(file) then
98
98
  [get_title(file, open(file).read), uri(file)]
99
- else [File::basename(file), uri(file)]
99
+ else
100
+ [escaped_basename(file), uri(file)]
100
101
  end
101
102
  }
102
103
  else
@@ -106,14 +107,14 @@ class Mamemose::Server
106
107
  Dir.entries(filename).each do |i|
107
108
  next if ignore?(i)
108
109
  link = uri(File.join(filename, i))
109
- if File.directory?(path(link)) then
110
- dirs << [File.basename(link) + File::SEPARATOR, link]
110
+ if File.directory?(fullpath(link)) then
111
+ dirs << [escaped_basename(link) + File::SEPARATOR, link]
111
112
  elsif markdown?(link)
112
- File.open(path(link)) do |f|
113
+ File.open(fullpath(link)) do |f|
113
114
  markdowns << [get_title(link, f.read), link]
114
115
  end
115
116
  else
116
- files << [File::basename(link), link]
117
+ files << [escaped_basename(link), link]
117
118
  end
118
119
  end
119
120
 
@@ -137,7 +138,7 @@ class Mamemose::Server
137
138
  if markdown?(req.path)
138
139
  str = file.read
139
140
  title = get_title(filename, str)
140
- res.body = header_html(title, req.path) + markdown(str) + footer_html(path(req.path))
141
+ res.body = header_html(title, req.path) + markdown(str) + footer_html(fullpath(req.path))
141
142
  res.content_type = CONTENT_TYPE
142
143
  else
143
144
  res.body = file.read
@@ -277,7 +278,7 @@ HTML
277
278
  link_str += File::SEPARATOR + "<a href='#{uri}'>#{s}</a>"
278
279
  end
279
280
  link_str += " <a class='filename' href=\"javascript:copy('#{docpath(uri)}');\">[copy]</a>"
280
- uri.gsub!('/'+File::basename(uri), "") if File.file?(path(uri))
281
+ uri.gsub!('/'+File::basename(uri), "") if File.file?(fullpath(uri))
281
282
  link_str = "<a href='/'>#{DOCUMENT_ROOT}</a>" + link_str
282
283
  search_form = <<HTML
283
284
  <form action="/search" method="get">
@@ -303,23 +304,41 @@ HTML
303
304
  return html
304
305
  end
305
306
 
307
+ # returns escaped characters so that the markdown parser doesn't interpret it has special meaning.
308
+ def escape(text)
309
+ return text.gsub(/[\`*_{}\[\]()#+\-.!]/, "\\\\\\0")
310
+ end
311
+
312
+ # returns /-rooted path. eg. /path/to/my_document.md
306
313
  def uri(path)
307
314
  s = File::expand_path(path).gsub(DIR, "").gsub(File::SEPARATOR, '/')
308
315
  return s == '' ? '/' : s
309
316
  end
310
317
 
311
- def path(uri)
318
+ # returns fullpath. eg. /home/daimatz/Dropbox/memo/path/to/my_document.md
319
+ def fullpath(uri)
312
320
  return File.join(DIR, uri.gsub('/', File::SEPARATOR))
313
321
  end
314
322
 
323
+ # returns DOCUMENT_ROOT-rooted path. eg. ~/Dropbox/memo/path/to/my_document.md
315
324
  def docpath(uri)
316
325
  return File.join(DOCUMENT_ROOT, uri.gsub('/', File::SEPARATOR)).gsub(/#{File::SEPARATOR}$/, "")
317
326
  end
318
327
 
328
+ # returns DOCUMENT_ROOT-rooted path, but escaped. eg. ~/Dropbox/memo/path/to/my\_document.md
329
+ # used in user-viewable (HTML) context.
330
+ def showpath(uri)
331
+ return escape(docpath(uri))
332
+ end
333
+
334
+ def escaped_basename(filename)
335
+ return escape(File::basename(filename))
336
+ end
337
+
319
338
  def link_list(title, link)
320
- file = path(link)
339
+ file = fullpath(link)
321
340
  str = File.file?(file) ? sprintf("%.1fKB", File.size(file) / 1024.0) : "dir"
322
- return "- [#{title}](#{link}) <a class='filename' href=\"javascript:copy('#{docpath(link)}');\">[#{File.basename(link)}, #{str}]</a>\n"
341
+ return "- [#{title}](#{link}) <a class='filename' href=\"javascript:copy('#{docpath(link)}');\">[#{escaped_basename(link)}, #{str}]</a>\n"
323
342
  end
324
343
 
325
344
  def markdown?(file)
@@ -336,9 +355,9 @@ HTML
336
355
  end
337
356
 
338
357
  def get_title(filename, str="")
339
- return File::basename(filename) if !markdown?(filename)
358
+ return escaped_basename(filename) if !markdown?(filename)
340
359
  title = str.split(/$/)[0]
341
- return title =~ /^\s*$/ ? File::basename(filename) : title
360
+ return title =~ /^\s*$/ ? escaped_basename(filename) : title
342
361
  end
343
362
 
344
363
  def markdown(text)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mamemose
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-07 00:00:00.000000000 Z
12
+ date: 2012-12-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redcarpet