Skip to content

Instantly share code, notes, and snippets.

@nbrew
Created February 2, 2011 20:30
Show Gist options
  • Save nbrew/808369 to your computer and use it in GitHub Desktop.
Save nbrew/808369 to your computer and use it in GitHub Desktop.
RefineryCMS: Create pages hierarchy from a simple text outline
#!/usr/local/bin/ruby
# Create/Load Pages from a text-based outline of titles.
# create this script in the root of your app
# otherwise, change the path to config/environment.
ENV["RAILS_ENV"] ||= 'development'
require File.dirname(__FILE__) + '/config/environment'
page_part_titles = Page.respond_to?(:default_page_parts) ? Page.default_page_parts : ['body','side_body']
nodes = {}
File.open('project_outline.txt', 'r') do |f1|
while line = f1.gets
# get the indentation and title
m = line.match(/([ \t]+)?(.*)$/)
next unless m[2]
indentation = m[1]
title = m[2]
# if we're at the root, set to 0
# otherwise, replace two spaces with a tab and count the tabs
level = indentation.nil? ? 0 : indentation.gsub(/ /,"\t").size
page = Page.find_or_create_by_title(title).parts
if level == 0
# don't bother to track every branch
nodes = {}
else
parent = nodes[level-1]
page.update_attribute(:parent_id, parent.id)
end
page_part_titles.each do |page_part_title|
page.parts.find_or_create_by_title(page_part_title) { |p| p.body = nil }
end
nodes[level] = page
puts "(#{level}): #{line}"
end
end
Home
About
Company
People
Management
Accounting
News
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment