2012-01-18 15:15:45 -05:00
|
|
|
class HelpController < ApplicationController
|
2015-09-08 09:42:30 -04:00
|
|
|
skip_before_action :authenticate_user!, :reject_blocked
|
|
|
|
|
2015-05-01 04:39:11 -04:00
|
|
|
layout 'help'
|
2015-04-30 15:28:07 -04:00
|
|
|
|
2012-01-18 15:15:45 -05:00
|
|
|
def index
|
2015-09-22 18:26:51 -04:00
|
|
|
@help_index = File.read(Rails.root.join('doc', 'README.md'))
|
2015-09-23 00:24:17 -04: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 15:15:45 -05:00
|
|
|
end
|
2013-06-06 06:19:23 -04:00
|
|
|
|
2014-04-18 11:21:21 -04:00
|
|
|
def show
|
2015-04-30 13:06:18 -04:00
|
|
|
@category = clean_path_info(path_params[:category])
|
|
|
|
@file = path_params[:file]
|
2014-04-18 11:21:21 -04:00
|
|
|
|
2015-04-15 12:45:31 -04:00
|
|
|
respond_to do |format|
|
|
|
|
format.any(:markdown, :md, :html) do
|
2015-08-18 12:08:03 -04:00
|
|
|
# Note: We are purposefully NOT using `Rails.root.join`
|
|
|
|
path = File.join(Rails.root, 'doc', @category, "#{@file}.md")
|
2015-04-15 12:45:31 -04: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
|
|
|
|
format.any(:png, :gif, :jpeg) do
|
2015-08-18 12:08:03 -04:00
|
|
|
# Note: We are purposefully NOT using `Rails.root.join`
|
|
|
|
path = File.join(Rails.root, 'doc', @category, "#{@file}.#{params[:format]}")
|
2015-04-15 12:45:31 -04: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 11:21:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-15 12:24:44 -04:00
|
|
|
def shortcuts
|
2013-06-30 15:10:52 -04:00
|
|
|
end
|
2015-03-08 17:46:22 -04:00
|
|
|
|
|
|
|
def ui
|
2016-04-16 16:00:30 -04:00
|
|
|
@user = User.new(id: 0, name: 'John Doe', username: '@johndoe')
|
2015-03-08 17:46:22 -04:00
|
|
|
end
|
2015-04-10 12:16:46 -04:00
|
|
|
|
2015-04-15 12:24:44 -04:00
|
|
|
private
|
|
|
|
|
2015-04-15 12:45:31 -04:00
|
|
|
def path_params
|
|
|
|
params.require(:category)
|
|
|
|
params.require(:file)
|
|
|
|
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
2015-04-10 12:16:46 -04:00
|
|
|
PATH_SEPS = Regexp.union(*[::File::SEPARATOR, ::File::ALT_SEPARATOR].compact)
|
|
|
|
|
2015-04-14 07:07:15 -04:00
|
|
|
# Taken from ActionDispatch::FileHandler
|
|
|
|
# Cleans up the path, to prevent directory traversal outside the doc folder.
|
2015-04-10 12:16:46 -04:00
|
|
|
def clean_path_info(path_info)
|
2015-04-14 07:07:15 -04:00
|
|
|
parts = path_info.split(PATH_SEPS)
|
2015-04-10 12:16:46 -04:00
|
|
|
|
|
|
|
clean = []
|
|
|
|
|
2015-04-14 07:07:15 -04:00
|
|
|
# Walk over each part of the path
|
2015-04-10 12:16:46 -04:00
|
|
|
parts.each do |part|
|
2015-04-14 07:07:15 -04:00
|
|
|
# Turn `one//two` or `one/./two` into `one/two`.
|
2015-04-10 12:16:46 -04:00
|
|
|
next if part.empty? || part == '.'
|
2015-04-14 07:07:15 -04: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 12:16:46 -04:00
|
|
|
end
|
|
|
|
|
2015-04-14 07:07:15 -04:00
|
|
|
# If the path was an absolute path (i.e. `/` or `/one/two`),
|
|
|
|
# add `/` to the front of the clean path.
|
2015-04-10 12:16:46 -04:00
|
|
|
clean.unshift '/' if parts.empty? || parts.first.empty?
|
|
|
|
|
2015-04-14 07:07:15 -04:00
|
|
|
# Join all the clean path parts by the path separator.
|
2015-04-10 12:16:46 -04:00
|
|
|
::File.join(*clean)
|
|
|
|
end
|
2012-01-18 15:15:45 -05:00
|
|
|
end
|