2012-01-18 20:15:45 +00:00
|
|
|
class HelpController < ApplicationController
|
2015-09-08 13:42:30 +00:00
|
|
|
skip_before_action :authenticate_user!, :reject_blocked
|
|
|
|
|
2015-05-01 08:39:11 +00:00
|
|
|
layout 'help'
|
2015-04-30 19:28:07 +00:00
|
|
|
|
2012-01-18 20:15:45 +00:00
|
|
|
def index
|
2015-09-22 22:26:51 +00:00
|
|
|
@help_index = File.read(Rails.root.join('doc', 'README.md'))
|
2015-09-23 04:24:17 +00:00
|
|
|
|
|
|
|
# Prefix Markdown links with `help/` unless they already have been
|
|
|
|
# See http://rubular.com/r/nwwhzH6Z8X
|
|
|
|
@help_index.gsub!(/(\]\()(?!help\/)([^\)\(]+)(\))/, '\1help/\2\3')
|
2012-01-18 20:15:45 +00:00
|
|
|
end
|
2013-06-06 10:19:23 +00:00
|
|
|
|
2014-04-18 15:21:21 +00:00
|
|
|
def show
|
2016-07-11 19:12:55 +00:00
|
|
|
@path = clean_path_info(path_params[:path])
|
2014-04-18 15:21:21 +00:00
|
|
|
|
2015-04-15 16:45:31 +00:00
|
|
|
respond_to do |format|
|
|
|
|
format.any(:markdown, :md, :html) do
|
2015-08-18 16:08:03 +00:00
|
|
|
# Note: We are purposefully NOT using `Rails.root.join`
|
2016-05-16 22:06:26 +00:00
|
|
|
path = File.join(Rails.root, 'doc', "#{@path}.md")
|
2015-04-15 16:45:31 +00:00
|
|
|
|
|
|
|
if File.exist?(path)
|
|
|
|
@markdown = File.read(path)
|
|
|
|
|
|
|
|
render 'show.html.haml'
|
|
|
|
else
|
|
|
|
# Force template to Haml
|
|
|
|
render 'errors/not_found.html.haml', layout: 'errors', status: 404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Allow access to images in the doc folder
|
2016-07-12 17:28:39 +00:00
|
|
|
format.any(:png, :gif, :jpeg, :mp4) do
|
2015-08-18 16:08:03 +00:00
|
|
|
# Note: We are purposefully NOT using `Rails.root.join`
|
2016-05-16 22:06:26 +00:00
|
|
|
path = File.join(Rails.root, 'doc', "#{@path}.#{params[:format]}")
|
2015-04-15 16:45:31 +00:00
|
|
|
|
|
|
|
if File.exist?(path)
|
|
|
|
send_file(path, disposition: 'inline')
|
|
|
|
else
|
|
|
|
head :not_found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Any other format we don't recognize, just respond 404
|
|
|
|
format.any { head :not_found }
|
2014-04-18 15:21:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-15 16:24:44 +00:00
|
|
|
def shortcuts
|
2013-06-30 19:10:52 +00:00
|
|
|
end
|
2015-03-08 21:46:22 +00:00
|
|
|
|
|
|
|
def ui
|
2016-04-16 20:00:30 +00:00
|
|
|
@user = User.new(id: 0, name: 'John Doe', username: '@johndoe')
|
2015-03-08 21:46:22 +00:00
|
|
|
end
|
2015-04-10 16:16:46 +00:00
|
|
|
|
2015-04-15 16:24:44 +00:00
|
|
|
private
|
|
|
|
|
2015-04-15 16:45:31 +00:00
|
|
|
def path_params
|
2016-05-16 22:06:26 +00:00
|
|
|
params.require(:path)
|
2015-04-15 16:45:31 +00:00
|
|
|
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
2015-04-10 16:16:46 +00:00
|
|
|
PATH_SEPS = Regexp.union(*[::File::SEPARATOR, ::File::ALT_SEPARATOR].compact)
|
|
|
|
|
2015-04-14 11:07:15 +00:00
|
|
|
# Taken from ActionDispatch::FileHandler
|
|
|
|
# Cleans up the path, to prevent directory traversal outside the doc folder.
|
2015-04-10 16:16:46 +00:00
|
|
|
def clean_path_info(path_info)
|
2015-04-14 11:07:15 +00:00
|
|
|
parts = path_info.split(PATH_SEPS)
|
2015-04-10 16:16:46 +00:00
|
|
|
|
|
|
|
clean = []
|
|
|
|
|
2015-04-14 11:07:15 +00:00
|
|
|
# Walk over each part of the path
|
2015-04-10 16:16:46 +00:00
|
|
|
parts.each do |part|
|
2015-04-14 11:07:15 +00:00
|
|
|
# Turn `one//two` or `one/./two` into `one/two`.
|
2015-04-10 16:16:46 +00:00
|
|
|
next if part.empty? || part == '.'
|
2015-04-14 11:07:15 +00:00
|
|
|
|
|
|
|
if part == '..'
|
|
|
|
# Turn `one/two/../` into `one`
|
|
|
|
clean.pop
|
|
|
|
else
|
|
|
|
# Add simple folder names to the clean path.
|
|
|
|
clean << part
|
|
|
|
end
|
2015-04-10 16:16:46 +00:00
|
|
|
end
|
|
|
|
|
2015-04-14 11:07:15 +00:00
|
|
|
# If the path was an absolute path (i.e. `/` or `/one/two`),
|
|
|
|
# add `/` to the front of the clean path.
|
2015-04-10 16:16:46 +00:00
|
|
|
clean.unshift '/' if parts.empty? || parts.first.empty?
|
|
|
|
|
2015-04-14 11:07:15 +00:00
|
|
|
# Join all the clean path parts by the path separator.
|
2015-04-10 16:16:46 +00:00
|
|
|
::File.join(*clean)
|
|
|
|
end
|
2012-01-18 20:15:45 +00:00
|
|
|
end
|