active_copy 1.0.0.pre
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rspec +1 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +116 -0
- data/MIT-LICENSE +20 -0
- data/README.md +77 -0
- data/Rakefile +9 -0
- data/active_copy.gemspec +27 -0
- data/config/initializers/register_active_copy_template_handler.rb +4 -0
- data/lib/active_copy.rb +21 -0
- data/lib/active_copy/attributes.rb +58 -0
- data/lib/active_copy/base.rb +34 -0
- data/lib/active_copy/finders.rb +61 -0
- data/lib/active_copy/markdown.rb +34 -0
- data/lib/active_copy/paths.rb +54 -0
- data/lib/active_copy/renderer.rb +13 -0
- data/lib/active_copy/source.rb +24 -0
- data/lib/active_copy/template.rb +20 -0
- data/lib/active_copy/version.rb +3 -0
- data/lib/active_copy/view_helper.rb +17 -0
- data/lib/generators/copy/USAGE +14 -0
- data/lib/generators/copy/copy_generator.rb +34 -0
- data/lib/generators/copy/templates/USAGE.erb +13 -0
- data/lib/generators/copy/templates/generator.rb.erb +39 -0
- data/lib/generators/copy/templates/model.rb.erb +3 -0
- data/lib/generators/copy/templates/test.rb.erb +5 -0
- data/lib/generators/copy/templates/view.md.erb +8 -0
- data/lib/tasks/active_copy_tasks.rake +4 -0
- data/spec/active_copy/.keep +0 -0
- data/spec/active_copy/base_spec.rb +31 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.keep +0 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +23 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +29 -0
- data/spec/dummy/config/environments/production.rb +80 -0
- data/spec/dummy/config/environments/test.rb +36 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +12 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +56 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/public/404.html +58 -0
- data/spec/dummy/public/422.html +58 -0
- data/spec/dummy/public/500.html +57 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/spec/fixtures/basic_pages/content/about.md +5 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/support/basic_page.rb +4 -0
- metadata +256 -0
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'redcarpet'
|
2
|
+
require 'active_copy/renderer'
|
3
|
+
|
4
|
+
# Compiles a Markdown file using the +Redcarpet+ template engine. Used
|
5
|
+
# by +ActionView+ in +config/initializers/markdown.rb+ to initiate
|
6
|
+
# Markdown template compilation for files that are not already
|
7
|
+
# precompiled.
|
8
|
+
module ActiveCopy
|
9
|
+
class Markdown
|
10
|
+
# Create a new session with the compiler.
|
11
|
+
def initialize
|
12
|
+
@renderer = ActiveCopy::Renderer.new
|
13
|
+
@options = {
|
14
|
+
autolink: true,
|
15
|
+
no_intra_emphasis: true,
|
16
|
+
fenced_code_blocks: true,
|
17
|
+
lax_html_blocks: true,
|
18
|
+
strikethrough: true,
|
19
|
+
superscript: true
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
# Return an HTML String containing the rendered output of the Markdown
|
24
|
+
# source.
|
25
|
+
def render markdown_source
|
26
|
+
markdown.render "#{markdown_source}"
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def markdown
|
31
|
+
@client ||= Redcarpet::Markdown.new @renderer, @options
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module ActiveCopy
|
2
|
+
module Paths
|
3
|
+
# Return absolute path to public HTML file.
|
4
|
+
def index_path
|
5
|
+
"#{path}/index.html"
|
6
|
+
end
|
7
|
+
|
8
|
+
# Return absolute path to public cached copy.
|
9
|
+
def path
|
10
|
+
@public_path ||= if Rails.env.test?
|
11
|
+
"#{Rails.root}/tmp/site/#{relative_path}"
|
12
|
+
else
|
13
|
+
"#{Rails.root}/public/#{relative_path}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Return the collection_path in the instance.
|
18
|
+
def collection_path
|
19
|
+
self.class.collection_path
|
20
|
+
end
|
21
|
+
|
22
|
+
# Return relative path with the Rails.root/public part out.
|
23
|
+
def relative_path
|
24
|
+
@rel_path ||= begin
|
25
|
+
date_array = id.split("-")[0..2]
|
26
|
+
date_path = date_array.join("/")
|
27
|
+
article_id = begin
|
28
|
+
str = id.gsub date_array.join("-"), ''
|
29
|
+
if str[0] == "-"
|
30
|
+
str[1..-1]
|
31
|
+
else
|
32
|
+
str
|
33
|
+
end
|
34
|
+
end
|
35
|
+
"#{category}/#{date_path}/#{article_id}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Return absolute path to Markdown file on this machine.
|
40
|
+
def source_path options={}
|
41
|
+
@source_path ||= if options[:relative]
|
42
|
+
File.join collection_path, "#{self.id}.md"
|
43
|
+
else
|
44
|
+
File.join root_path, collection_path, "#{self.id}.md"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
def root_path
|
50
|
+
return Rails.root if defined? Rails
|
51
|
+
File.expand_path "../../", __FILE__
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'pygments'
|
2
|
+
require 'redcarpet'
|
3
|
+
|
4
|
+
module ActiveCopy
|
5
|
+
class Renderer < Redcarpet::Render::HTML
|
6
|
+
def block_code(raw_code, language)
|
7
|
+
code = "\n#{raw_code.strip}"
|
8
|
+
#Rails.logger.info "Rendered highlighted #{language} code block"
|
9
|
+
#Pygments.highlight code, lexer: language
|
10
|
+
"<pre>#{code}\n</pre>"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
|
2
|
+
# Methods for extracting the source of the Markdown document.
|
3
|
+
|
4
|
+
module ActiveCopy
|
5
|
+
module Source
|
6
|
+
# Test if the source file is present on this machine.
|
7
|
+
def present?
|
8
|
+
File.exists? source_path
|
9
|
+
end
|
10
|
+
|
11
|
+
# Return the raw String source of the Markdown document, including
|
12
|
+
# the YAML front matter.
|
13
|
+
def raw_source
|
14
|
+
@raw ||= File.read source_path
|
15
|
+
end
|
16
|
+
|
17
|
+
# Return the String source of the Markdown document without the YAML
|
18
|
+
# front matter. This is what is passed into ActionView to render the
|
19
|
+
# Markdown from this file.
|
20
|
+
def source
|
21
|
+
@source ||= raw_source.split("---\n")[2]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# The ActionView template handler that parses Markdown files into HTML
|
2
|
+
# using our custom Markdown handler. Also checks for YAML front matter
|
3
|
+
# and parses it out before rendering the Markdown source into HTML.
|
4
|
+
module ActiveCopy
|
5
|
+
module Template
|
6
|
+
def self.call template
|
7
|
+
source = if template.respond_to? :split
|
8
|
+
template.split("---\n")[2]
|
9
|
+
else
|
10
|
+
template.source.split("---\n")[2]
|
11
|
+
end
|
12
|
+
|
13
|
+
<<-RUBY
|
14
|
+
markdown = ActiveCopy::Markdown.new
|
15
|
+
markdown.render %(#{source})
|
16
|
+
RUBY
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ActiveCopy
|
2
|
+
module ViewHelper
|
3
|
+
# Render a given relative content path to Markdown.
|
4
|
+
def render_copy from_source_path
|
5
|
+
source_path = "#{ActiveCopy.content_path}/#{from_source_path}.md"
|
6
|
+
|
7
|
+
if File.exists? source_path
|
8
|
+
raw_source = IO.read source_path
|
9
|
+
source = raw_source.split("---\n")[2]
|
10
|
+
template = ActiveCopy::Markdown.new
|
11
|
+
template.render(source).html_safe
|
12
|
+
else
|
13
|
+
raise ArgumentError.new "#{source_path} does not exist."
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Description:
|
2
|
+
Generates a new ActiveCopy model for use in this project. Specify
|
3
|
+
attributes that the model will use in the arguments following the
|
4
|
+
first.
|
5
|
+
|
6
|
+
Example:
|
7
|
+
rails generate copy article name title tags category date
|
8
|
+
|
9
|
+
This will create:
|
10
|
+
app/models/article.rb
|
11
|
+
spec/models/article_spec.rb
|
12
|
+
lib/generators/article/article_generator.rb
|
13
|
+
lib/generators/article/templates/view.md.erb
|
14
|
+
lib/generators/article/USAGE
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class CopyGenerator < ::Rails::Generators::NamedBase
|
2
|
+
source_root File.expand_path("../templates", __FILE__)
|
3
|
+
desc "Creates a new ActiveCopy model"
|
4
|
+
class_option :datestamp, \
|
5
|
+
desc: "Add a date stamp to the beginnings of file names.",
|
6
|
+
default: false
|
7
|
+
|
8
|
+
def model_class
|
9
|
+
template "app/models/#{file_name}.rb", 'model.rb.erb'
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_file
|
13
|
+
template "spec/models/#{file_name}_spec.rb", 'test.rb.erb'
|
14
|
+
end
|
15
|
+
|
16
|
+
def model_generator
|
17
|
+
template "#{generator_dir}/#{file_name}_generator.rb", 'generator.rb.erb'
|
18
|
+
template "#{generator_dir}/templates/view.md.erb", 'view.md.erb'
|
19
|
+
template "#{generator_dir}/USAGE", "USAGE.erb"
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def file_name
|
24
|
+
human_name.parameterize
|
25
|
+
end
|
26
|
+
|
27
|
+
def table_name
|
28
|
+
human_name.tableize
|
29
|
+
end
|
30
|
+
|
31
|
+
def generator_dir
|
32
|
+
"lib/generators/#{file_name}"
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
Description:
|
2
|
+
Add a description for your ActiveCopy model generator here.
|
3
|
+
|
4
|
+
Example:
|
5
|
+
rails generate copy <%= file_name %>
|
6
|
+
|
7
|
+
<% if datestamped? %>
|
8
|
+
This will create:
|
9
|
+
app/views/<%= table_name %>/content/2000-01-01-name.md
|
10
|
+
<% else %>
|
11
|
+
This will create:
|
12
|
+
app/views/<%= table_name %>/content/name.md
|
13
|
+
<% end %>
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class <%= model_name %>Generator < ::Rails::Generators::NamedBase
|
2
|
+
source_root File.expand_path("../templates", __FILE__)
|
3
|
+
desc "Creates a new <%= model_name %>"
|
4
|
+
|
5
|
+
def generate_view
|
6
|
+
template "app/views/#{directory}/content/#{file_name}.md", 'view.md.erb'
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
def date
|
11
|
+
@date_string ||= Time.now.strftime '%Y-%m-%d'
|
12
|
+
end
|
13
|
+
|
14
|
+
def title
|
15
|
+
@title ||= param_name.titleize.downcase
|
16
|
+
end
|
17
|
+
|
18
|
+
def table_name
|
19
|
+
human_name.tableize
|
20
|
+
end
|
21
|
+
|
22
|
+
def param_name
|
23
|
+
human_name.parameterize
|
24
|
+
end
|
25
|
+
|
26
|
+
def file_name
|
27
|
+
<% if datestamped? %>
|
28
|
+
"#{date}-#{param_name}"
|
29
|
+
<% else %>
|
30
|
+
param_name
|
31
|
+
<% end %>
|
32
|
+
end
|
33
|
+
|
34
|
+
def attributes_yaml
|
35
|
+
attributes.reduce "" do |yaml, key|
|
36
|
+
yaml << "#{key}: \n"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
nd
|
File without changes
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ActiveCopy
|
4
|
+
describe Base do
|
5
|
+
let(:page) { BasicPage.new id: "about" }
|
6
|
+
|
7
|
+
it "finds the page by its id" do
|
8
|
+
expect(page).to be_present, "Page was not found"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "finds the right folder to read source files from" do
|
12
|
+
expect(page.collection_path).to eq("spec/fixtures/basic_pages/content")
|
13
|
+
end
|
14
|
+
|
15
|
+
it "reads the yaml front matter as a hash" do
|
16
|
+
expect(page.title).to eq('About Us')
|
17
|
+
end
|
18
|
+
|
19
|
+
it "reads the article body" do
|
20
|
+
expect(page.source).to match(/We are a very serious company/)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns the correct partial path" do
|
24
|
+
expect(page.to_partial_path).to eq("basic_pages/basic_page")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "finds the basic_page that we just made" do
|
28
|
+
expect(BasicPage.all.map(&:id)).to include(page.id)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/spec/dummy/Rakefile
ADDED
File without changes
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the top of the
|
9
|
+
* compiled file, but it's generally better to create a new file per style scope.
|
10
|
+
*
|
11
|
+
*= require_self
|
12
|
+
*= require_tree .
|
13
|
+
*/
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
|
6
|
+
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|