Themes.pm 8.4 KB

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