diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f3b8b6dd9e..931e472c60 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,6 +1,4 @@ ---- -title: Contributing Guide ---- +# Contributing Guide ## Code of Conduct diff --git a/README.md b/README.md index 3a9be7c4d0..14bc0d1eac 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,4 @@ ---- -title: Administrate -home: true ---- +# Administrate [![CircleCI](https://img.shields.io/circleci/project/github/thoughtbot/administrate.svg)](https://circleci.com/gh/thoughtbot/administrate/tree/master) [![Gem Version](https://badge.fury.io/rb/administrate.svg)](https://badge.fury.io/rb/administrate) diff --git a/spec/example_app/app/controllers/docs_controller.rb b/spec/example_app/app/controllers/docs_controller.rb index bdea74c234..0beddd436f 100644 --- a/spec/example_app/app/controllers/docs_controller.rb +++ b/spec/example_app/app/controllers/docs_controller.rb @@ -1,97 +1,32 @@ class DocsController < ApplicationController - SPECIAL_FILES = [ - { - file: 'CONTRIBUTING', - page: 'contributing' - } - ].freeze - - REDCARPET_CONFIG = { - fenced_code_blocks: true, - autolink: true, - }.freeze - def index - render_page "README" + render_page("README") end def show - render_correct_page - end - - private - - def find_special_file - SPECIAL_FILES.select { |page| page[:page] == params[:page] }.first - end - - def render_correct_page - if !find_special_file.nil? - render_page(find_special_file[:file]) + case params[:page] + when "contributing" + render_page("CONTRIBUTING", "Contributing Guide") else render_page("docs/#{params[:page]}") end end - def render_page(name) - path = full_page_path(name) + private + + def render_page(name, title = nil) + page = DocPage.find(name) - if File.exist?(path) - contents = parse_document(path) - @page_title = contents.title - @page_title_suffix = contents.title_suffix + if page + title = title || page.title + @page_title = [title, "Administrate"].compact.join(" - ") # rubocop:disable Rails/OutputSafety - render layout: "docs", html: contents.body.html_safe + render layout: "docs", html: page.body.html_safe # rubocop:enable Rails/OutputSafety else - render file: "#{Rails.root}/public/404.html", + render file: Rails.root.join("public", "404.html"), layout: false, status: :not_found end end - - def full_page_path(page) - Rails.root + "../../#{page}.md" - end - - def parse_document(path) - text = File.read(path) - DocumentParser.new(text) - end - - class DocumentParser - def initialize(source_text) - front_matter_parsed = FrontMatterParser::Parser.new(:md).call(source_text) - @source_text = front_matter_parsed.content - @metadata = front_matter_parsed.front_matter - end - - def body - @body ||= - begin - renderer = Redcarpet::Render::HTML - markdown = Redcarpet::Markdown.new(renderer, REDCARPET_CONFIG) - - source_text_with_heading = <<~MARKDOWN - # #{title} - - #{source_text} - MARKDOWN - - markdown.render(source_text_with_heading) - end - end - - def title - metadata["title"] - end - - def title_suffix - metadata["home"] ? "" : " - Administrate" - end - - private - - attr_reader :source_text, :metadata - end end diff --git a/spec/example_app/app/models/doc_page.rb b/spec/example_app/app/models/doc_page.rb new file mode 100644 index 0000000000..4e318c65cf --- /dev/null +++ b/spec/example_app/app/models/doc_page.rb @@ -0,0 +1,71 @@ +class DocPage + class << self + def find(page) + full_path = Rails.root + "../../#{page}.md" + + if File.exist?(full_path) + text = File.read(full_path) + new(text) + end + end + end + + def initialize(text) + @text = text + end + + def title + document.title + end + + def body + document.body + end + + private + + attr_reader :text + + def document + @document ||= DocumentParser.new(text) + end + + class DocumentParser + def initialize(source_text) + parsed_document = FrontMatterParser::Parser.new(:md).call(source_text) + @source_text = parsed_document.content + @metadata = parsed_document.front_matter + end + + def body + @body ||= + begin + renderer = Redcarpet::Render::HTML + markdown = Redcarpet::Markdown.new(renderer, redcarpet_config) + + source_text_with_heading = <<~MARKDOWN + # #{title} + + #{source_text} + MARKDOWN + + markdown.render(source_text_with_heading) + end + end + + def title + metadata["title"] + end + + private + + attr_reader :source_text, :metadata + + def redcarpet_config + { + fenced_code_blocks: true, + autolink: true, + } + end + end +end diff --git a/spec/example_app/app/views/layouts/docs.html.erb b/spec/example_app/app/views/layouts/docs.html.erb index 042ef2cfab..e84d974dc1 100644 --- a/spec/example_app/app/views/layouts/docs.html.erb +++ b/spec/example_app/app/views/layouts/docs.html.erb @@ -2,7 +2,7 @@
-Administrate is " \ + "released as a Ruby gem", + ), + ) + end + end +end