Skip to content

Commit

Permalink
Add ability to extend tag-sets
Browse files Browse the repository at this point in the history
  • Loading branch information
dwarring committed Sep 3, 2024
1 parent 19d0f45 commit f7a9ac5
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 61 deletions.
10 changes: 3 additions & 7 deletions lib/CSS/TagSet.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ use CSS::Media;
use CSS::Properties;
use CSS::Stylesheet;
use CSS::Writer;
use CSS::Units :pt;

sub load-css-tagset($tag-css, Str :$media-type, |c) is export(:load-css-tagset) {
my %asts;
sub load-css-tagset($tag-css, CSS::Media :$media, :%tags, |c) is export(:load-css-tagset) {
with $tag-css {
# Todo: load via CSS::Stylesheet?
my $css = .IO.slurp;
my CSS::Media $media .= new: :type($_), :width(595pt), :height(842pt)
with $media-type;
my CSS::Stylesheet $style-sheet .= parse: $css, :$media, |c;

for $style-sheet.rules {
Expand All @@ -30,7 +26,7 @@ sub load-css-tagset($tag-css, Str :$media-type, |c) is export(:load-css-tagset)
}

my $key = @path == 1 ?? @path.head !! CSS::Writer.write: :selector($_);
%asts{$key}.append: $declarations.map: {:property($_)};
%tags{$key}.append: $declarations.map: {:property($_)};
}
}
}
Expand All @@ -40,7 +36,7 @@ sub load-css-tagset($tag-css, Str :$media-type, |c) is export(:load-css-tagset)
note "running with 'raku --doc', I hope"
}

%asts;
%tags;
}

method xpath-init($) {} # override me
Expand Down
37 changes: 20 additions & 17 deletions lib/CSS/TagSet/Pango.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,11 @@ use CSS::Units;

has CSS::Module $.module = CSS::Module::CSS3.module;
has CSS::Properties %!props;
has %!tags;

constant %Tags is export(:PangoTags) = load-css-tagset(%?RESOURCES<pango.css>);
method declarations { %Tags }
constant %BaseTags is export(:PangoTags) = load-css-tagset(%?RESOURCES<pango.css>);

method base-style(Str $prop) {
%!props{$prop} //= CSS::Properties.new(:$!module, declarations => %Tags{$prop}) // [];
}

# mapping of Pango attributes to CSS properties
has %!SpanProp = %(
background => 'background-color',
'face'|'font_family' => 'font-family',
foreground => 'color',
stretch => 'font-stretch',
style => 'font-style',
weight => 'font-weight',
);
submethod TWEAK {

my %CustomProps = %(
rise => '-pango-rise' => %(
:synopsis<integer>,
Expand Down Expand Up @@ -95,8 +81,25 @@ submethod TWEAK {
$!module.extend(:$name, |$meta);
%!SpanProp{$att} = $name;
}
%!tags = %BaseTags;
}

method declarations { %!tags }

method base-style(Str $tag) {
%!props{$tag} //= CSS::Properties.new(:$!module, declarations => %!tags{$tag}) // [];
}

# mapping of Pango attributes to CSS properties
has %!SpanProp = %(
background => 'background-color',
'face'|'font_family' => 'font-family',
foreground => 'color',
stretch => 'font-stretch',
style => 'font-style',
weight => 'font-weight',
);

# Builds CSS properties from an element from a tag name and attributes
multi method tag-style('span', *%attrs) {
my CSS::Properties $css = self.base-style('span').clone;
Expand All @@ -115,7 +118,7 @@ multi method tag-style('span', *%attrs) {
}

multi method tag-style($tag) {
self.base-style($tag);
self.base-style($tag).clone;
}

=begin pod
Expand Down
27 changes: 15 additions & 12 deletions lib/CSS/TagSet/TaggedPDF.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@ unit class CSS::TagSet::TaggedPDF;
use CSS::TagSet :&load-css-tagset;
also does CSS::TagSet;

use CSS::Media;
use CSS::Module;
use CSS::Module::CSS3;
use CSS::Properties;
use CSS::Units :pt;

has CSS::Module $.module = CSS::Module::CSS3.module;
has CSS::Properties %!props;
has %!tags;

constant %Tags is export(:PDFTags) = load-css-tagset(%?RESOURCES<tagged-pdf.css>, :xml, :media-type<print> );
constant $media = CSS::Media.new: :type<print>, :width(595pt), :height(842pt);
constant %BaseTags is export(:PDFTags) = load-css-tagset(%?RESOURCES<tagged-pdf.css>, :xml, :$media );

method declarations { %Tags }

method base-style(Str $prop) {
%!props{$prop} //= CSS::Properties.new: :$!module, declarations => %Tags{$prop} // [];
}

submethod TWEAK {
submethod TWEAK(IO() :$style-sheet) {
my %CustomProps = %(
'-pdf-space-before'|'-pdf-space-after'|'-pdf-start-indent'|'-pdf-end-indent' => %(
:synopsis<number>,
Expand All @@ -30,7 +28,15 @@ submethod TWEAK {
for %CustomProps.pairs {
$!module.extend(:name(.key), |.value);
}
%!tags = %BaseTags;
load-css-tagset($_, :xml, :$media, :%!tags )
with $style-sheet;
}

method declarations { %!tags }

method base-style(Str $tag) {
%!props{$tag} //= CSS::Properties.new: :$!module, declarations => %!tags{$tag} // [];
}

sub snake-case($s) {
Expand Down Expand Up @@ -72,10 +78,7 @@ our %Layout = %(

my subset HashMap of Pair where .value ~~ Associative;
# Builds CSS properties from an element from a tag name and attributes
multi method tag-style(Str $tag, *% where !.so) {
self.base-style($tag).clone;
}
multi method tag-style($tag, *%attrs --> CSS::Properties:D) {
method tag-style($tag, *%attrs --> CSS::Properties:D) {
my CSS::Properties $css = self.base-style($tag).clone;

for %attrs.keys.grep({%Layout{$_}:exists}) -> $key {
Expand Down
53 changes: 28 additions & 25 deletions lib/CSS/TagSet/XHTML.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,35 @@ use URI;
has CSS::Properties %!props;
has SetHash %!link-pseudo;
has CSS::Module $.module = CSS::Module::CSS3.module;
has %!tags;

constant %Tags is export(:Tags) = load-css-tagset(%?RESOURCES<xhtml.css>);
constant %BaseTags is export(:Tags) = load-css-tagset(%?RESOURCES<xhtml.css>);

method declarations { %Tags }
submethod TWEAK(IO() :$style-sheet) {
my %CustomProps = %(
'-xhtml-align' => %(
:like<text-align>,
),
'-xhtml-colspan'|'-xhtml-rowspan' => %(
:synopsis<integer>,
:default(1),
:coerce(-> Int() $num { :$num }),
),
);

for %CustomProps.pairs {
$!module.extend(:name(.key), |.value);
}
%!tags = %BaseTags;
load-css-tagset($_, :%!tags )
with $style-sheet;

}

method declarations { %!tags }

method base-style(Str $prop) {
%!props{$prop} //= CSS::Properties.new(:$!module, declarations => %Tags{$prop} // []);
%!props{$prop} //= CSS::Properties.new(:$!module, declarations => %!tags{$prop} // []);
}

# mapping of HTML attributes to CSS properties
Expand Down Expand Up @@ -54,28 +76,12 @@ method xpath-init($xpath-context) {
? ($elem.tag ~~ 'a'|'link'|'area' && self.link-pseudo($name, $elem));
});
}
submethod TWEAK(:$xpath-context) {
my %CustomProps = %(
'-xhtml-align' => %(
:like<text-align>,
),
'-xhtml-colspan'|'-xhtml-rowspan' => %(
:synopsis<integer>,
:default(1),
:coerce(-> Int() $num { :$num }),
),
);

for %CustomProps.pairs {
$!module.extend(:name(.key), |.value);
}
}

# any additional CSS styling based on HTML attributes
multi sub tweak-style('bdo', $css) {
$css.unicode-bidi //= :keyw<bidi-override>;
}
multi sub tweak-style($, $,) is default {
multi sub tweak-style($, $,) {
}

sub matching-media($media, $query) {
Expand Down Expand Up @@ -122,12 +128,9 @@ method stylesheet-content($doc, :$media, :$links) {
}

# Builds CSS properties from an element from a tag name and attributes
multi method tag-style(Str $tag, *% where !.so) {
self.base-style($tag).clone;
}
multi method tag-style(Str $tag, :$hidden, *%attrs) {
method tag-style(Str $tag, :$hidden, *%attrs) {
my CSS::Properties $css = self.base-style($tag).clone;
$css.display = :keyw<none> with $hidden;
$css.display = :keyw<none> if $hidden;

for %attrs.keys.grep({%AttrTags{$_}:exists && $tag ~~ %AttrTags{$_}}) {
my $name = %AttrProp{$_} // $_;
Expand Down
1 change: 1 addition & 0 deletions t/pdf-extra.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Code { font-size: 0.7em; font-style: italic }
5 changes: 5 additions & 0 deletions t/tag-set-pdf.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ is $tag-set.tag-style('LI'), 'display:list-item; list-style:none; mar
is $tag-set.tag-style('Span', :BorderStyle<Dotted>), 'border:dotted;', '<Dotted/>';
is $tag-set.tag-style('Span', :SpaceBefore(5)), '-pdf-space-before:5pt;', '<Span SpaceBefore=...>';

is $tag-set.tag-style('Code'), 'font:0.85em monospace; white-space:pre;', 'Base Code style';

$tag-set .= new: :style-sheet<t/pdf-extra.css>;
is $tag-set.tag-style('Code'), 'font:italic 0.7em monospace; white-space:pre;', 'Extended Code style';

done-testing();
5 changes: 5 additions & 0 deletions t/tag-set-xhtml.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ is $tag-set.tag-style('small'), 'font-size:0.75em;';
$tag-set.base-style('blah').font-weight = 'bold';
is $tag-set.tag-style('blah'), 'font-weight:bold;';

is $tag-set.tag-style('kbd'), 'font-family:monospace;', 'Base kbd style';

$tag-set .= new: :style-sheet<t/xhtml-extra.css>;
is $tag-set.tag-style('kbd'), 'font:0.85em monospace;', 'Extended kbd style';

done-testing();
1 change: 1 addition & 0 deletions t/xhtml-extra.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kbd { font-size: .85em }

0 comments on commit f7a9ac5

Please sign in to comment.