Themes.pm 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package Trog::Themes;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures state};
  6. use FindBin::libs;
  7. use Trog::Vars;
  8. use Trog::Config;
  9. =head1 Trog::Themes
  10. Utility functions for getting themed paths.
  11. =cut
  12. our $template_dir = 'www/templates';
  13. =head2 routes() = %routes
  14. The active theme's own routing table, from its routes.pm.
  15. Themes can add routes of their own by shipping a routes.pm which populates
  16. %Theme::routes. Returns an empty list when there's no theme, or when the theme
  17. hasn't got one. Dies if there is a routes.pm and it won't load, as silently
  18. serving a site with half its routes missing is worse than not starting.
  19. =cut
  20. sub routes {
  21. my $rdir = get_dir();
  22. return () unless -f "$rdir/routes.pm";
  23. local $@;
  24. eval { require "$rdir/routes.pm"; 1; } or do {
  25. die "Could not load Theme routing package: $@";
  26. };
  27. return %Theme::routes;
  28. }
  29. =head2 get_dir() = STRING $dir
  30. The active theme's directory, relative to the tCMS root, e.g.
  31. C<www/themes/mytheme>. Empty string when no theme is configured, or when the
  32. configured one doesn't exist on disk -- so it's safe to interpolate either way,
  33. and truthiness is the test for "is there a theme".
  34. Memoized for the life of the process: changing the theme goes through
  35. /config/save, which signals the workers to restart.
  36. =cut
  37. sub get_dir {
  38. state $tdir = '';
  39. return $tdir if $tdir;
  40. my $conf = Trog::Config::get();
  41. my $theme = $conf->param('general.theme') || '';
  42. if( $theme ) {
  43. my $themedir = "www/themes/$theme";
  44. $tdir = $themedir if -d $themedir;
  45. }
  46. return $tdir;
  47. };
  48. =head2 td() = STRING $dir
  49. get_dir() as a rooted URL path rather than a filesystem one, for templates to
  50. build asset links with. Empty string when there's no theme.
  51. =cut
  52. sub td {
  53. my $dir = get_dir();
  54. return $dir ? "/$dir" : '';
  55. }
  56. # The theme's template dir and the stock one, in that order of preference.
  57. # The theme entry is empty when no theme is configured, so callers can just
  58. # grep for truth rather than repeating the "is there a theme" dance.
  59. sub _template_dirs ( $content_type, $is_component = 0 ) {
  60. my $ct = $Trog::Vars::byct{$content_type};
  61. my $theme = get_dir();
  62. my ( $themed, $stock ) = ( $theme ? "$theme/templates/$ct" : '', "$template_dir/$ct" );
  63. if ($is_component) {
  64. $themed .= "/components" if $themed;
  65. $stock .= "/components";
  66. }
  67. return ( $themed, $stock );
  68. }
  69. =head2 template_dirs($content_type, $is_component)
  70. Every directory which could satisfy a template, most specific first.
  71. Hand this to Text::Xslate as its C<path> and a theme gets to override
  72. individual templates without having to fork every last one of them.
  73. =cut
  74. sub template_dirs ( $content_type, $is_component = 0 ) {
  75. return grep { $_ && -d $_ } _template_dirs( $content_type, $is_component );
  76. }
  77. =head2 template_dir($template, $content_type, $is_component, $is_dir)
  78. The single directory which owns $template: the theme's if it has its own copy,
  79. the stock one otherwise. With $is_dir, $template names a subdirectory (forms,
  80. headers, footers) rather than a file.
  81. This answers "who owns this one thing", which is what you want when *writing*.
  82. For reading a whole directory you almost always want themed_templates_in_dir()
  83. instead, which spans both rather than making you pick.
  84. =cut
  85. sub template_dir ( $template, $content_type, $is_component = 0, $is_dir = 0 ) {
  86. my ( $mtd, $mtemp ) = _template_dirs( $content_type, $is_component );
  87. if ($is_dir) {
  88. return $mtd && -d "$mtd/$template" ? $mtd : $mtemp;
  89. }
  90. return $mtd && -f "$mtd/$template" ? $mtd : $mtemp;
  91. }
  92. # Pick appropriate dir based on whether theme override exists
  93. sub _dir_for_resource ($resource) {
  94. my $theme_dir = get_dir();
  95. return $theme_dir && -f "$theme_dir/$resource" ? $theme_dir : '';
  96. }
  97. =head2 themed($resource)
  98. Path to a www/-relative resource, preferring the theme's copy.
  99. =cut
  100. sub themed ($resource) {
  101. return _dir_for_resource("$resource") . "/$resource";
  102. }
  103. =head2 themed_style($resource) = @hrefs
  104. Stylesheets are the exception to the "one or the other" rule: you get the stock
  105. sheet *and* the theme's, in that order, so a theme can override a handful of
  106. rules without restating the whole thing.
  107. =cut
  108. # For style we want to load *both* style files and have the override come later.
  109. sub themed_style ($resource) {
  110. my @styles = ("/styles/$resource");
  111. my $styled = _dir_for_resource("styles/$resource");
  112. $styled =~ s/^www\///;
  113. push( @styles, "/$styled/styles/$resource" ) if $styled;
  114. return @styles;
  115. }
  116. =head2 themed_script($resource)
  117. Path to a script under www/scripts, preferring the theme's copy.
  118. =cut
  119. sub themed_script ($resource) {
  120. return _dir_for_resource("scripts/$resource") . "/scripts/$resource";
  121. }
  122. =head2 themed_template($resource)
  123. Path to a template under www/templates, preferring the theme's copy.
  124. =cut
  125. sub themed_template ($resource) {
  126. return _dir_for_resource("templates/$resource") . "/templates/$resource";
  127. }
  128. =head2 dir_for($path, $content_type, $is_component)
  129. The single directory which owns a given component subdir: the theme's if it has
  130. one, the stock one otherwise. This is the right answer for I<writing> -- see
  131. forms_dir() and the post type wizard. For reading, you almost certainly want
  132. themed_templates_in_dir() instead, which spans both.
  133. =cut
  134. sub dir_for ( $path, $ct, $is_component = 0 ) {
  135. return template_dir( $path, $ct, $is_component, 1 ) . "/$path";
  136. }
  137. =head2 forms_dir()
  138. dir_for() the post type forms directory, which is where the post type wizard
  139. writes new types and their schema sidecars.
  140. =cut
  141. sub forms_dir {
  142. return dir_for( 'forms', 'text/html', 1 );
  143. }
  144. =head2 files_in_dir($path, $content_type, $is_component, $ext)
  145. Bare filenames with the given extension in whichever single directory dir_for()
  146. picks. See themed_templates_in_dir() for the version which spans both.
  147. =cut
  148. sub files_in_dir ( $path, $ct, $is_component = 0, $ext = 'tx' ) {
  149. return _read_dir( dir_for( $path, $ct, $is_component ), $ext );
  150. }
  151. sub _read_dir ( $dir, $ext ) {
  152. my $files = [];
  153. return $files unless -d $dir;
  154. opendir( my $dh, $dir ) or return $files;
  155. while ( my $file = readdir($dh) ) {
  156. push( @$files, $file ) if -f "$dir/$file" && $file =~ m/\.\Q$ext\E$/;
  157. }
  158. closedir($dh);
  159. return $files;
  160. }
  161. =head2 templates_in_dir($path, $content_type, $is_component)
  162. files_in_dir() restricted to templates.
  163. Beware that this only ever reads one directory -- see themed_templates_in_dir()
  164. for why that's usually not what you want.
  165. =cut
  166. sub templates_in_dir ( $path, $ct, $is_component = 0 ) {
  167. return files_in_dir( $path, $ct, $is_component, 'tx' );
  168. }
  169. =head2 themed_templates_in_dir($path, $content_type, $is_component)
  170. Same calling convention as templates_in_dir(), but it doesn't make you pick a
  171. side: you get the theme's templates where the theme has them, plus the stock
  172. ones it hasn't overridden.
  173. templates_in_dir() resolves through template_dir(), which is winner-take-all --
  174. if a theme has a forms/ dir at all, its contents are the *only* post types the
  175. site can see, and every core one vanishes. That's rarely what a theme author
  176. means by overriding a template.
  177. Returns bare filenames, deduped, theme first. Which of the two files a given
  178. name resolves to is then up to whoever renders it -- see template_dirs().
  179. =cut
  180. sub themed_templates_in_dir ( $path, $ct, $is_component = 0 ) {
  181. my @dirs = grep { $_ } map { "$_/$path" } _template_dirs( $ct, $is_component );
  182. my ( @templates, %seen );
  183. foreach my $dir (@dirs) {
  184. foreach my $template ( @{ _read_dir( $dir, 'tx' ) } ) {
  185. # First one wins, and the theme's dir comes first.
  186. push( @templates, $template ) unless $seen{$template}++;
  187. }
  188. }
  189. return \@templates;
  190. }
  191. =head2 themed_file_in_dir($path, $file, $content_type, $is_component)
  192. The path to a single named file in an abstract template dir, preferring the
  193. theme's copy. Returns undef when neither has it.
  194. =cut
  195. sub themed_file_in_dir ( $path, $file, $ct, $is_component = 0 ) {
  196. foreach my $dir ( grep { $_ } _template_dirs( $ct, $is_component ) ) {
  197. return "$dir/$path/$file" if -f "$dir/$path/$file";
  198. }
  199. return undef;
  200. }
  201. 1;