Themes.pm 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package Trog::Themes;
  2. use 5.040;
  3. use re '/aasx';
  4. use warnings FATAL => 'all';
  5. use FindBin::libs;
  6. use Path::Tiny();
  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"; ## no critic (ProhibitFiletest_f) -- theme ships routes, or it does not
  23. # get_dir() is relative to the tCMS root, which require only searches from '.'.
  24. local @INC = ( '.', @INC );
  25. local $@;
  26. eval { require "$rdir/routes.pm"; 1; } or do { ## no critic (Modules::RequireBarewordIncludes) -- a theme ships a file, not a module
  27. die "Could not load Theme routing package: $@";
  28. };
  29. no warnings 'once';
  30. return %Theme::routes;
  31. }
  32. =head2 get_dir() = STRING $dir
  33. The active theme's directory, relative to the tCMS root, e.g.
  34. C<www/themes/mytheme>. Empty string when no theme is configured, or when the
  35. configured one doesn't exist on disk -- so it's safe to interpolate either way,
  36. and truthiness is the test for "is there a theme".
  37. Memoized for the life of the process: changing the theme goes through
  38. /config/save, which signals the workers to restart.
  39. =cut
  40. sub get_dir {
  41. state $tdir = '';
  42. return $tdir if $tdir;
  43. my $conf = Trog::Config::get();
  44. my $theme = $conf->param('general.theme') || '';
  45. if ($theme) {
  46. my $themedir = "www/themes/$theme";
  47. $tdir = $themedir if -d $themedir;
  48. }
  49. return $tdir;
  50. }
  51. =head2 td() = STRING $dir
  52. get_dir() as a rooted URL path rather than a filesystem one, for templates to
  53. build asset links with. Empty string when there's no theme.
  54. =cut
  55. sub td {
  56. my $dir = get_dir();
  57. return $dir ? "/$dir" : '';
  58. }
  59. # The theme's template dir and the stock one, in that order of preference.
  60. # The theme entry is empty when no theme is configured, so callers can just
  61. # grep for truth rather than repeating the "is there a theme" dance.
  62. my sub _template_dirs ( $content_type, $is_component = 0 ) {
  63. my $ct = $Trog::Vars::byct{$content_type};
  64. my $theme = get_dir();
  65. my ( $themed, $stock ) = ( $theme ? "$theme/templates/$ct" : '', "$template_dir/$ct" );
  66. if ($is_component) {
  67. $themed .= "/components" if $themed;
  68. $stock .= "/components";
  69. }
  70. return ( $themed, $stock );
  71. }
  72. =head2 template_dirs($content_type, $is_component)
  73. Every directory which could satisfy a template, most specific first.
  74. Hand this to Text::Xslate as its C<path> and a theme gets to override
  75. individual templates without having to fork every last one of them.
  76. =cut
  77. sub template_dirs ( $content_type, $is_component = 0 ) {
  78. return grep { $_ && -d $_ } _template_dirs( $content_type, $is_component );
  79. }
  80. =head2 template_dir($template, $content_type, $is_component, $is_dir)
  81. The single directory which owns $template: the theme's if it has its own copy,
  82. the stock one otherwise. With $is_dir, $template names a subdirectory (forms,
  83. headers, footers) rather than a file.
  84. This answers "who owns this one thing", which is what you want when *writing*.
  85. For reading a whole directory you almost always want themed_templates_in_dir()
  86. instead, which spans both rather than making you pick.
  87. =cut
  88. sub template_dir ( $template, $content_type, $is_component = 0, $is_dir = 0 ) {
  89. my ( $mtd, $mtemp ) = _template_dirs( $content_type, $is_component );
  90. if ($is_dir) {
  91. return $mtd && -d "$mtd/$template" ? $mtd : $mtemp;
  92. }
  93. return $mtd && -f "$mtd/$template" ? $mtd : $mtemp; ## no critic (ProhibitFiletest_f) -- which dir owns the template
  94. }
  95. # Pick appropriate dir based on whether theme override exists
  96. my sub dir_for_resource ($resource) {
  97. my $theme_dir = get_dir();
  98. return $theme_dir && -f "$theme_dir/$resource" ? $theme_dir : ''; ## no critic (ProhibitFiletest_f) -- theme override, else stock
  99. }
  100. =head2 themed($resource)
  101. Path to a www/-relative resource, preferring the theme's copy.
  102. =cut
  103. sub themed ($resource) {
  104. return dir_for_resource("$resource") . "/$resource";
  105. }
  106. =head2 themed_style($resource) = @hrefs
  107. Stylesheets are the exception to the "one or the other" rule: you get the stock
  108. sheet *and* the theme's, in that order, so a theme can override a handful of
  109. rules without restating the whole thing.
  110. =cut
  111. # For style we want to load *both* style files and have the override come later.
  112. sub themed_style ($resource) {
  113. my @styles = ("/styles/$resource");
  114. my $styled = dir_for_resource("styles/$resource");
  115. $styled =~ s/^www\///;
  116. push( @styles, "/$styled/styles/$resource" ) if $styled;
  117. return @styles;
  118. }
  119. =head2 themed_script($resource)
  120. Path to a script under www/scripts, preferring the theme's copy.
  121. =cut
  122. sub themed_script ($resource) {
  123. return dir_for_resource("scripts/$resource") . "/scripts/$resource";
  124. }
  125. =head2 themed_template($resource)
  126. Path to a template under www/templates, preferring the theme's copy.
  127. =cut
  128. sub themed_template ($resource) {
  129. return dir_for_resource("templates/$resource") . "/templates/$resource";
  130. }
  131. =head2 dir_for($path, $content_type, $is_component)
  132. The single directory which owns a given component subdir: the theme's if it has
  133. one, the stock one otherwise. This is the right answer for I<writing> -- see
  134. forms_dir() and the post type wizard. For reading, you almost certainly want
  135. themed_templates_in_dir() instead, which spans both.
  136. =cut
  137. sub dir_for ( $path, $ct, $is_component = 0 ) {
  138. return template_dir( $path, $ct, $is_component, 1 ) . "/$path";
  139. }
  140. =head2 forms_dir()
  141. dir_for() the post type forms directory, which is where the post type wizard
  142. writes new types and their schema sidecars.
  143. =cut
  144. sub forms_dir {
  145. return dir_for( 'forms', 'text/html', 1 );
  146. }
  147. my sub read_dir ( $dir, $ext ) {
  148. return [] unless -d $dir;
  149. my @files = eval {
  150. grep { -f } Path::Tiny::path($dir)->children(qr/[.]\Q$ext\E\z/);
  151. }; ## no critic (ProhibitFiletest_f) -- listing names, nothing is opened
  152. return [ map { $_->basename } @files ];
  153. }
  154. =head2 files_in_dir($path, $content_type, $is_component, $ext)
  155. Bare filenames with the given extension in whichever single directory dir_for()
  156. picks. See themed_templates_in_dir() for the version which spans both.
  157. =cut
  158. sub files_in_dir ( $path, $ct, $is_component = 0, $ext = 'tx' ) {
  159. return read_dir( dir_for( $path, $ct, $is_component ), $ext );
  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"; ## no critic (ProhibitFiletest_f) -- search path, first hit wins
  198. }
  199. return undef;
  200. }
  201. =head2 themed_component($file, $content_type)
  202. The path to a file sitting directly in the components dir rather than in one of
  203. its subdirectories, preferring the theme's copy. Returns undef when neither has
  204. it.
  205. This is themed_file_in_dir() for the components which are not post types: the
  206. canned editor blocks every form splices in, and the JSON sidecars beside them
  207. which say what each one collects -- see Trog::DataModule::include_schema_for().
  208. =cut
  209. sub themed_component ( $file, $ct = 'text/html' ) {
  210. foreach my $dir ( template_dirs( $ct, 1 ) ) {
  211. return "$dir/$file" if -f "$dir/$file"; ## no critic (ProhibitFiletest_f) -- search path, first hit wins
  212. }
  213. return undef;
  214. }
  215. 1;