Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels committed Feb 2, 2009
0 parents commit 029ad85
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README
@@ -0,0 +1,19 @@
= Share Snippets

Exmaple configuration:

---
- :name: header
:file: /Users/james/header.txt
:title: Confluence
:breadcrumb: Confluence
:slug: /wiki
:before: <html><head></head><body>
:after:
- :name: footer
:file: /Users/james/footer.txt
:title: Confluence
:breadcrumb: Confluence
:slug: /wiki
:before:
:after: </body></html>
120 changes: 120 additions & 0 deletions Rakefile
@@ -0,0 +1,120 @@
# I think this is the one that should be moved to the extension Rakefile template

# In rails 1.2, plugins aren't available in the path until they're loaded.
# Check to see if the rspec plugin is installed first and require
# it if it is. If not, use the gem version.

# Determine where the RSpec plugin is by loading the boot
unless defined? RADIANT_ROOT
ENV["RAILS_ENV"] = "test"
case
when ENV["RADIANT_ENV_FILE"]
require File.dirname(ENV["RADIANT_ENV_FILE"]) + "/boot"
when File.dirname(__FILE__) =~ %r{vendor/radiant/vendor/extensions}
require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../../")}/config/boot"
else
require "#{File.expand_path(File.dirname(__FILE__) + "/../../../")}/config/boot"
end
end

require 'rake'
require 'rake/rdoctask'
require 'rake/testtask'

rspec_base = File.expand_path(RADIANT_ROOT + '/vendor/plugins/rspec/lib')
$LOAD_PATH.unshift(rspec_base) if File.exist?(rspec_base)
require 'spec/rake/spectask'
# require 'spec/translator'

# Cleanup the RADIANT_ROOT constant so specs will load the environment
Object.send(:remove_const, :RADIANT_ROOT)

extension_root = File.expand_path(File.dirname(__FILE__))

task :default => :spec
task :stats => "spec:statsetup"

desc "Run all specs in spec directory"
Spec::Rake::SpecTask.new(:spec) do |t|
t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
t.spec_files = FileList['spec/**/*_spec.rb']
end

namespace :spec do
desc "Run all specs in spec directory with RCov"
Spec::Rake::SpecTask.new(:rcov) do |t|
t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
t.spec_files = FileList['spec/**/*_spec.rb']
t.rcov = true
t.rcov_opts = ['--exclude', 'spec', '--rails']
end

desc "Print Specdoc for all specs"
Spec::Rake::SpecTask.new(:doc) do |t|
t.spec_opts = ["--format", "specdoc", "--dry-run"]
t.spec_files = FileList['spec/**/*_spec.rb']
end

[:models, :controllers, :views, :helpers].each do |sub|
desc "Run the specs under spec/#{sub}"
Spec::Rake::SpecTask.new(sub) do |t|
t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"]
end
end

# Hopefully no one has written their extensions in pre-0.9 style
# desc "Translate specs from pre-0.9 to 0.9 style"
# task :translate do
# translator = ::Spec::Translator.new
# dir = RAILS_ROOT + '/spec'
# translator.translate(dir, dir)
# end

# Setup specs for stats
task :statsetup do
require 'code_statistics'
::STATS_DIRECTORIES << %w(Model\ specs spec/models)
::STATS_DIRECTORIES << %w(View\ specs spec/views)
::STATS_DIRECTORIES << %w(Controller\ specs spec/controllers)
::STATS_DIRECTORIES << %w(Helper\ specs spec/views)
::CodeStatistics::TEST_TYPES << "Model specs"
::CodeStatistics::TEST_TYPES << "View specs"
::CodeStatistics::TEST_TYPES << "Controller specs"
::CodeStatistics::TEST_TYPES << "Helper specs"
::STATS_DIRECTORIES.delete_if {|a| a[0] =~ /test/}
end

namespace :db do
namespace :fixtures do
desc "Load fixtures (from spec/fixtures) into the current environment's database. Load specific fixtures using FIXTURES=x,y"
task :load => :environment do
require 'active_record/fixtures'
ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
(ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(RAILS_ROOT, 'spec', 'fixtures', '*.{yml,csv}'))).each do |fixture_file|
Fixtures.create_fixtures('spec/fixtures', File.basename(fixture_file, '.*'))
end
end
end
end
end

desc 'Generate documentation for the share_layouts extension.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'SaveOutSnippetsExtension'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end

# For extensions that are in transition
desc 'Test the share_layouts extension.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

# Load any custom rakefiles for extension
Dir[File.dirname(__FILE__) + '/tasks/*.rake'].sort.each { |f| require f }
2 changes: 2 additions & 0 deletions app/controllers/admin/share_snippets_controller.rb
@@ -0,0 +1,2 @@
class Admin::ShareSnippetsController < ApplicationController
end
2 changes: 2 additions & 0 deletions app/helpers/admin/share_snippets_helper.rb
@@ -0,0 +1,2 @@
module Admin::ShareSnippetsHelper
end
10 changes: 10 additions & 0 deletions app/views/admin/share_snippets/index.rhtml
@@ -0,0 +1,10 @@
<h1>Snippet sharing</h1>
<p>The snippet sharing plugin will save your snippets out to specified locations when modified. Great for integrating your site's theme with other applications.</p>

<h2>How to configure</h2>

<p>Snippet sharing can be configured in: <pre><%= SaveOutSnippetsExtension::config_path %></pre></p>

<h2>Current configuration</h2>

<%= debug SaveOutSnippetsExtension::current_config %>
28 changes: 28 additions & 0 deletions lib/tasks/share_layouts_extension_tasks.rake
@@ -0,0 +1,28 @@
namespace :radiant do
namespace :extensions do
namespace :share_layouts do

desc "Runs the migration of the Save Out Snippets extension"
task :migrate => :environment do
require 'radiant/extension_migrator'
if ENV["VERSION"]
ShareLayoutsExtension.migrator.migrate(ENV["VERSION"].to_i)
else
ShareLayoutsExtension.migrator.migrate
end
end

desc "Copies public assets of the Save Out Snippets to the instance public/ directory."
task :update => :environment do
is_svn_or_dir = proc {|path| path =~ /\.svn/ || File.directory?(path) }
Dir[ShareLayoutsExtension.root + "/public/**/*"].reject(&is_svn_or_dir).each do |file|
path = file.sub(ShareLayoutsExtension.root, '')
directory = File.dirname(path)
puts "Copying #{path}..."
mkdir_p RAILS_ROOT + directory
cp file, RAILS_ROOT + path
end
end
end
end
end
51 changes: 51 additions & 0 deletions save_out_snippets_extension.rb
@@ -0,0 +1,51 @@
require_dependency 'application'

module ShareSnippets
module SnippetExt
def after_save
share_config = SaveOutSnippetsExtension::current_config

for share in share_config.select {|s| s[:name] == name}

# create a dummy page to our specifications so that we can render the radius tags
new_page = Page.new(:title => share[:title], :slug => share[:slug], :breadcrumb => share[:breadcrumb])
new_page.created_at = new_page.updated_at = Time.now

# write to file, inserting the extra markup specified
File.open(share[:file], 'w') {|f| f.write([share[:before], new_page.render_snippet(self), share[:after]]) }
end

end
end
end

class SaveOutSnippetsExtension < Radiant::Extension

version "1.0"
description "This plugin shares your Radiant Snippets with other programs"
url "http://www.marginleft.com"

define_routes do |map|
map.connect 'admin/share_snippets/:action', :controller => 'admin/share_snippets'
end

def activate
base_config = []
File.open(config_path, 'w') {|f| f.write(base_config.to_yaml) } if !File.exist?(config_path)
admin.tabs.add "Snippet Sharing", "/admin/share_snippets", :after => "Blog Feeds", :visibility => [:all]
Snippet.send :include, ShareSnippets::SnippetExt
end

def deactivate
admin.tabs.remove "Snippet Sharing"
end

def config_path
defined?(SHARE_SNIPPETS_CONFIG_FILE_PATH) ? SHARE_SNIPPETS_CONFIG_FILE_PATH : "#{RAILS_ROOT}/config/share_snippets.yml"
end

def current_config
YAML::load(File.open(config_path))
end

end

0 comments on commit 029ad85

Please sign in to comment.