Skip to content

Commit

Permalink
Revamp inline and structure types
Browse files Browse the repository at this point in the history
Based on a re-read of the spec"
  • Loading branch information
dwarring committed Aug 29, 2024
1 parent 4c67fcc commit 6acf46f
Show file tree
Hide file tree
Showing 4 changed files with 361 additions and 371 deletions.
99 changes: 47 additions & 52 deletions lib/CSS/TagSet.rakumod
Original file line number Diff line number Diff line change
@@ -1,69 +1,64 @@
use v6;

# interface role for tagsets
role CSS::TagSet:ver<0.1.2> {
use CSS::Properties;
use CSS::Stylesheet;
use CSS::Writer;

sub load-css-tagset($tag-css, |c) is export(:load-css-tagset) {
my %asts;
with $tag-css {
# Todo: load via CSS::Stylesheet?
my CSS::Module $module = CSS::Module::CSS3.module;
my $actions = $module.actions.new: |c;
my $p = $module.grammar.parsefile(.IO.absolute, :$actions);
my %ast = $p.ast;

for %ast<stylesheet>.list {
with .<ruleset> {
my $declarations = .<declarations>;
for .<selectors>.list {
given .<selector> {
my @path;
for .list {
for .<simple-selector>.list {
@path.push: $_
with .<qname><element-name>;
}
#| interface role for tagsets
unit role CSS::TagSet:ver<0.1.2>;

use CSS::Properties;
use CSS::Stylesheet;
use CSS::Writer;

sub load-css-tagset($tag-css, |c) is export(:load-css-tagset) {
my %asts;
with $tag-css {
# Todo: load via CSS::Stylesheet?
my $css = .IO.slurp;
my CSS::Stylesheet $style-sheet .= parse: $css, |c;

for $style-sheet.rules {
with .ast<ruleset> {
my $declarations = .<declarations>;
for .<selectors>.list {
given .<selector> {
my @path;
for .list {
for .<simple-selector>.list {
@path.push: $_
with .<qname><element-name>;
}

my $key = @path == 1 ?? @path.head !! CSS::Writer.write: :selector($_);
%asts{$key}.append: $declarations.list;
}

my $key = @path == 1 ?? @path.head !! CSS::Writer.write: :selector($_);
%asts{$key}.append: $declarations.map: {:property($_)};
}
}
}
}
else {
note "running with 'raku --doc', I hope"
}

%asts;
}

method xpath-init($) {} # override me
method stylesheet-content($) { [] } # override me
method module { ... }
method stylesheet($doc, :$media, Bool :$links = False, |c --> CSS::Stylesheet) {
my @styles = @.stylesheet-content($doc, :$media, :$links);
my CSS::Stylesheet $css .= new: :$media, |c;
$css.parse($_) for @styles;
$css;
else {
note "running with 'raku --doc', I hope"
}

# attribute that contains inline styling
method inline-style-attribute { 'style' }
%asts;
}

# method to extract inline styling
method inline-style(Str $, Str :$style) {
CSS::Properties.new(:$.module, :$style);
}
method xpath-init($) {} # override me
method stylesheet-content($) { [] } # override me
method module { ... }
method stylesheet($doc, :$media, Bool :$links = False, |c --> CSS::Stylesheet) {
my @styles = @.stylesheet-content($doc, :$media, :$links);
my CSS::Stylesheet $css .= new: :$media, |c;
$css.parse($_) for @styles;
$css;
}

method base-style(|c) { ... }
# attribute that contains inline styling
method inline-style-attribute { 'style' }

# method to extract inline styling
method inline-style(Str $, Str :$style) {
CSS::Properties.new(:$.module, :$style);
}

method base-style(|c) { ... }

=begin pod
=head2 Name
Expand Down
207 changes: 103 additions & 104 deletions lib/CSS/TagSet/Pango.rakumod
Original file line number Diff line number Diff line change
@@ -1,122 +1,121 @@
use v6;
unit class CSS::TagSet::Pango;

use CSS::TagSet :&load-css-tagset;
also does CSS::TagSet;

class CSS::TagSet::Pango does CSS::TagSet {
use CSS::Module;
use CSS::Module::CSS3;
use CSS::Properties;
use CSS::Units;
use CSS::Module;
use CSS::Module::CSS3;
use CSS::Properties;
use CSS::Units;

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

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

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

my %CustomProps = %(
rise => '-pango-rise' => %(
:synopsis<integer>,
:default(0),
:coerce(-> Int:D() $num { :$num }),
),
fallback => '-pango-fallback' => %(
:synopsis('True | False'),
:default<False>,
:coerce(-> Str:D $att { my $num = ($att ~~ /:i 'True'/) ?? 1 !! 0; :$num }),
),
size => 'font-size' => %(
# map Pango `size` attribute to CSS `font-size` property
:like<font-size>,
:synopsis("<num> | xx-small | x-small | small | medium | large | x-large | xx-large | smaller | larger"),
:coerce(
-> $_ {
when CSS::Units:D {
$_;
}
when .lc set <xx-small x-small small medium large x-large xx-large smaller larger> {
:keyw(.lc);
}
default {
# size in thousandths of a point
my $pt = .Numeric / 1000;
:$pt
}
# 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>,
:default(0),
:coerce(-> Int:D() $num { :$num }),
),
fallback => '-pango-fallback' => %(
:synopsis('True | False'),
:default<False>,
:coerce(-> Str:D $att { my $num = ($att ~~ /:i 'True'/) ?? 1 !! 0; :$num }),
),
size => 'font-size' => %(
# map Pango `size` attribute to CSS `font-size` property
:like<font-size>,
:synopsis("<num> | xx-small | x-small | small | medium | large | x-large | xx-large | smaller | larger"),
:coerce(
-> $_ {
when CSS::Units:D {
$_;
}
when .lc set <xx-small x-small small medium large x-large xx-large smaller larger> {
:keyw(.lc);
}
default {
# size in thousandths of a point
my $pt = .Numeric / 1000;
:$pt
}
}),
),
variant => 'font-variant' => %(
:like<font-variant>,
:synopsis("normal | smallcaps"),
:coerce(
-> Str:D $att {
my $keyw = $att.lc;
$keyw ~~ s/smallcaps/small-caps/;
:$keyw
if $keyw set <normal small-caps>;
}),
),
variant => 'font-variant' => %(
:like<font-variant>,
:synopsis("normal | smallcaps"),
:coerce(
-> Str:D $att {
my $keyw = $att.lc;
$keyw ~~ s/smallcaps/small-caps/;
:$keyw
if $keyw set <normal small-caps>;
}),
),
strikethrough => 'text-decoration' => %(
:like<text-decoration>,
:synopsis("true | false"),
:coerce(
-> $_ {
if $_ ~~ CSS::Units {
$_;
}
else {
:keyw(
/:i true/ ?? 'line-through' !! 'none'
)
}
),
strikethrough => 'text-decoration' => %(
:like<text-decoration>,
:synopsis("true | false"),
:coerce(
-> $_ {
if $_ ~~ CSS::Units {
$_;
}
),
else {
:keyw(
/:i true/ ?? 'line-through' !! 'none'
)
}
}
),
);

for %CustomProps.pairs {
my $att := .key;
my Str $name := .value.key;
my Hash $meta := .value.value;
$!module.extend(:$name, |$meta);
%!SpanProp{$att} = $name;
}
),
);

for %CustomProps.pairs {
my $att := .key;
my Str $name := .value.key;
my Hash $meta := .value.value;
$!module.extend(:$name, |$meta);
%!SpanProp{$att} = $name;
}
}

# 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;

for %attrs.keys {
if %!SpanProp{$_}:exists {
my $name = %!SpanProp{$_};
$css."$name"() = %attrs{$_};
}
else {
warn "ignoring 'style' attribute: '$_'";
}
}
# 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;

$css;
for %attrs.keys {
if %!SpanProp{$_}:exists {
my $name = %!SpanProp{$_};
$css."$name"() = %attrs{$_};
}
else {
warn "ignoring 'style' attribute: '$_'";
}
}

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

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

=begin pod
Expand Down
Loading

0 comments on commit 6acf46f

Please sign in to comment.