#!/usr/bin/perl -w # # Movable Type (r) (C) 2001-2008 Six Apart, Ltd. All Rights Reserved. # This code cannot be redistributed without permission from www.sixapart.com. # For more information, consult your Movable Type license. # # Author: Byrne Reese # # $Id$ package MT::Tool::ExportTemplateSet; use strict; use warnings; use lib qw( lib extlib ); use base qw( MT::Tool ); sub usage { return qq{--blog= --name= [--version=] [--id=] [--key=] [--static=]}; } sub help { return qq{ export-ts - A tool to export a blog's templates as a template set --blog The Blog ID to export templates from. --id The ID of the resulting template set. (optional) --name The name to be used for the creation of the resulting plugin. This is also used to determine the output directory for related files. --version The version string to be used for the creation of the resulting plugin. (optional) --static The path to the directory containing your mt-static files for this template set. It must be a relative path from your mt-static folder. --key The MT::PluginData key of the resulting template set. (optional) --verbose Show verbose messages. }; } my ( $BLOG_ID, $TS_NAME, $TS_ID, $TS_KEY, $TS_VERSION, $VERBOSE, $BASE_DIR, $STATIC ); $TS_NAME = 'My Template Set'; $TS_VERSION = '1.01'; sub options { return ( 'blog=i' => \$BLOG_ID, 'name=s' => \$TS_NAME, 'static=s' => \$STATIC, 'id=s' => \$TS_ID, 'key=s' => \$TS_KEY, 'version=s' => \$TS_VERSION, ); } use MT::Template; use MT::TemplateMap; use YAML::Tiny; use File::Spec; use File::Path; use MT::Util qw( dirify ); use MT; use File::Copy::Recursive qw(dircopy); sub main { my $class = shift; $VERBOSE = $class->SUPER::main(@_); $class->show_usage(), exit if !$BLOG_ID || !$TS_NAME; $TS_ID ||= dirify($TS_NAME); $TS_KEY ||= $TS_ID; $STATIC ||= File::Spec->catdir( 'plugins', $TS_ID ); $BASE_DIR = $TS_ID . "-" . $TS_VERSION; mkpath( File::Spec->catdir( $BASE_DIR, 'plugins', $TS_ID, 'templates' ) ); my $mt = MT->new() or die MT->errstr; my $from = File::Spec->catdir( _static_file_path($mt) , $STATIC ); if (-e $from) { my $to = File::Spec->catdir( $BASE_DIR, 'mt-static', $STATIC ); debug( 'Copying static files from ' . $from . ' to ' . $to ); mkpath( $to ); dircopy( $from, $to ); } debug( 'Exporting templates from blog #' . $BLOG_ID ); my @tmpls; my $yaml = YAML::Tiny->new; $yaml->[0] = { name => $TS_NAME, id => $TS_ID, key => $TS_KEY, description => 'A Movable Type Template Set.', template_sets => { $TS_ID => { label => $TS_NAME, base_path => 'templates', } } }; my $ts = $yaml->[0]->{template_sets}->{$TS_ID}; # Index Templates @tmpls = MT::Template->load( { blog_id => $BLOG_ID, type => 'index', } ); for my $t (@tmpls) { debug( ' - Creating index template: ' . $t->name ); $ts->{templates}->{index}->{ $t->identifier } = { label => $t->name, outfile => $t->outfile, rebuild_me => $t->rebuild_me, }; } # Template Modules @tmpls = MT::Template->load( { blog_id => $BLOG_ID, type => 'custom', } ); for my $t (@tmpls) { debug( ' - Creating template module: ' . $t->name ); my $id = $t->identifier; $id ||= dirify($t->name); $ts->{templates}->{module}->{ $id } = { label => $t->name, }; write_tmpl($t); } # Widgets @tmpls = MT::Template->load( { blog_id => $BLOG_ID, type => 'widget', } ); for my $t (@tmpls) { debug( ' - Creating widget: ' . $t->name ); my $id = $t->identifier; $id ||= dirify($t->name); $ts->{templates}->{widget}->{ $id } = { label => $t->name, }; write_tmpl($t); } # Widget Sets @tmpls = MT::Template->load( { blog_id => $BLOG_ID, type => 'widgetset', } ); for my $t (@tmpls) { debug( ' - Creating widget set: ' . $t->name ); my $raw = $t->text; my $id = dirify($t->name); my @widgets = ($raw =~ /widget=\"([^\"]*)\"/g); $ts->{templates}->{widgetset}->{ $id } = { order => 1000, label => $t->name, widgets => \@widgets, }; } # System Templates @tmpls = MT::Template->load( [ { blog_id => $BLOG_ID, type => 'popup_image' } => -or => { blog_id => $BLOG_ID, type => 'dynamic_error' } => -or => { blog_id => $BLOG_ID, type => 'search_results' } => -or => { blog_id => $BLOG_ID, type => 'comment_preview' } => -or => { blog_id => $BLOG_ID, type => 'comment_response' } ] ); for my $t (@tmpls) { debug( ' - Creating system template: ' . $t->name ); $ts->{templates}->{system}->{ $t->type } = { label => $t->name, }; write_tmpl($t); } # Archive @tmpls = MT::Template->load( [ { blog_id => $BLOG_ID, type => 'archive' } => -or => { blog_id => $BLOG_ID, type => 'individual' } ] ); for my $t (@tmpls) { $ts->{templates}->{ $t->type }->{ $t->identifier }->{label} = $t->name; my @maps = MT::TemplateMap->load( { template_id => $t->id, } ); for my $map (@maps) { my $type = lc( $map->archive_type ); $type =~ s/ /-/g; debug( ' - Creating template map: ' . $type ); $ts->{templates}->{ $t->type }->{ $t->identifier }->{mappings} ->{$type} = { archive_type => $map->archive_type, preferred => $map->is_preferred, $map->file_template && $map->file_template ne '~' && $map->file_template ne '' ? ( file_template => $map->file_template ) : (), }; } write_tmpl($t); } debug(' - Writing config.yaml file'); $yaml->write( File::Spec->catfile( $BASE_DIR, 'plugins', $TS_ID, 'config.yaml' ) ); } sub write_tmpl { my ($tmpl) = @_; my $id = $tmpl->identifier; $id ||= dirify($tmpl->name); my $fn = File::Spec->catfile( $BASE_DIR, 'plugins', $TS_ID, 'templates', $id . '.mtml' ); open FILE, ">$fn"; print FILE $tmpl->text; close FILE; } sub debug { print $_[0] . "\n" if $VERBOSE; } sub _static_file_path { my ($ctx) = @_; my $cfg = $ctx->{cfg}; my $path = $cfg->StaticFilePath; if (!$path) { $path = $ctx->{mt_dir}; $path .= '/' unless $path =~ m!/$!; $path .= 'mt-static/'; } $path .= '/' unless $path =~ m!/$!; return $path; } __PACKAGE__->main() unless caller; 1; __END__ =head1 NAME export-ts - A tool to export a blog's templates as a template set =head1 SYNOPSIS cd /path/to/cgi-bin/mt perl ./tools/export_ts -blog=1 -id="MyTemplateSet" =head1 INSTALLATION Place this script inside your Movable Type "tools" directory. =head1 DESCRIPTION I is a tool to export a blog's templates to a template set that can easily be install elsewhere. =head1 OPTIONS The following options are available: --blog The Blog ID to export templates from --id The ID to be used for the creation of the resulting plugin. This is also used to determine the output directory for related files. =head1 USAGE From the command line, one would type: > chmod a+x export-ts > MT_HOME=/path/to/mt export-ts --blog= --id=MySet --name="My Template Set" This would result in the following directories being created: * MySet/plugins/MySet/config.yaml * MySet/plugins/MySet/templates/* * MySet/mt-static/plugins/MySet/* You should then be able to zip up the MySet directory or simply install its contents into MT_HOME as you would any other plugin. =cut