| 1 | package Term::Fabulous::Document; | ||
| 2 | |||
| 3 | 1 | ▶ | use v5.32; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use feature 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | no warnings 'experimental::signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use Object::Pad 0.825; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 0 | ▶ | use Term::Fabulous::Widget::Text; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | |||
| 12 | our $VERSION = '0.05'; | ||
| 13 | |||
| 14 | # The marker of a list item: a text a search passes over, since the | ||
| 15 | # bullets and numbers of a list are not content. | ||
| 16 | class Term::Fabulous::Document::Marker :isa(Term::Fabulous::Widget::Text) :strict(params) { | ||
| 17 | |||
| 18 | method search_texts :override () { | ||
| 19 | return (); | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | class Term::Fabulous::Document :strict(params) { | ||
| 24 | use Clay::UI::Role::Layout::HasParent qw(is_widget); | ||
| 25 | use Clay::XS qw(sizing_fixed sizing_grow CLAY_LEFT_TO_RIGHT CLAY_TOP_TO_BOTTOM); | ||
| 26 | use List::Util qw(first); | ||
| 27 | use Scalar::Util qw(blessed refaddr weaken); | ||
| 28 | use Term::Fabulous::Check qw(boolean describe non_negative_integer positive_integer string); | ||
| 29 | use Term::Fabulous::Document::Look; | ||
| 30 | use Term::Fabulous::Role::Searchable qw(searchables_in); | ||
| 31 | use Term::Fabulous::Text::Markup qw(parse_markup); | ||
| 32 | use Term::Fabulous::Unicode qw(string_columns); | ||
| 33 | use Term::Fabulous::Widget::Box; | ||
| 34 | use Term::Fabulous::Widget::CodeBlock; | ||
| 35 | use Term::Fabulous::Widget::Divider; | ||
| 36 | |||
| 37 | my @OPTIONS = qw(gap indent flush quote rows section anchor); | ||
| 38 | my %OPTION = map { $_ => 1 } @OPTIONS; | ||
| 39 | |||
| 40 | field $source :param :reader = undef; | ||
| 41 | field $look :param :reader = 'Term::Fabulous::Document::Look'; | ||
| 42 | |||
| 43 | # The items, how the view lays each out ({ gap, indent, flush, quote, | ||
| 44 | # rows }), and what a factory needs to know of the item before the | ||
| 45 | # next one ({ kind, indent, position }). | ||
| 46 | field @items; | ||
| 47 | field @options; | ||
| 48 | field @facts; | ||
| 49 | field %index_of; # refaddr of an item => its index | ||
| 50 | |||
| 51 | field @sections; # [ item index, level, title ] | ||
| 52 | field @anchors; # [ name, item index ] | ||
| 53 | field @code_blocks; # the code blocks the factories made | ||
| 54 | |||
| 55 | # The view that shows the document, weak; one at a time. | ||
| 56 | field $view; | ||
| 57 | |||
| 58 | ADJUST { | ||
| 59 | $source = string( $self, source => $source ) if defined $source; | ||
| 60 | die "Term::Fabulous::Document: look must be the name of a loaded Term::Fabulous::Document::Look class, got " . describe($look) | ||
| 61 | unless defined $look && !ref $look && length $look && $look->isa('Term::Fabulous::Document::Look'); | ||
| 62 | } | ||
| 63 | |||
| 64 | # --------------------------------------------------------------------- | ||
| 65 | # Items | ||
| 66 | # --------------------------------------------------------------------- | ||
| 67 | |||
| 68 | method add ( $widget, %given ) { | ||
| 69 | die "Term::Fabulous::Document: add needs a widget, got " . describe($widget) | ||
| 70 | unless is_widget($widget); | ||
| 71 | my $known = $index_of{ refaddr $widget }; | ||
| 72 | die "Term::Fabulous::Document: the widget is item $known of the document already" if defined $known; | ||
| 73 | die "Term::Fabulous::Document: add needs a widget without a parent; this one is a child of " . ref( $widget->parent ) if defined $widget->parent; | ||
| 74 | my @unknown = sort grep { !$OPTION{$_} } keys %given; | ||
| 75 | die "Term::Fabulous::Document: unknown option(s) @unknown (known: @OPTIONS)" if @unknown; | ||
| 76 | |||
| 77 | my %option = ( | ||
| 78 | gap => non_negative_integer( $self, gap => $given{gap} // 1 ), | ||
| 79 | indent => non_negative_integer( $self, indent => $given{indent} // 0 ), | ||
| 80 | flush => boolean( $self, flush => $given{flush} // 0 ), | ||
| 81 | quote => non_negative_integer( $self, quote => $given{quote} // 0 ), | ||
| 82 | rows => defined $given{rows} ? non_negative_integer( $self, rows => $given{rows} ) : undef, | ||
| 83 | ); | ||
| 84 | my $index = scalar @items; | ||
| 85 | my $section = defined $given{section} ? $self->_checked_section_option( $given{section} ) : undef; | ||
| 86 | my $anchor = defined $given{anchor} ? _normalized( string( $self, anchor => $given{anchor} ) ) : undef; | ||
| 87 | |||
| 88 | push @items, $widget; | ||
| 89 | push @options, \%option; | ||
| 90 | push @facts, { kind => 'widget', indent => $option{indent}, position => 1 }; | ||
| 91 | push @sections, [ $index, @$section ] if defined $section; | ||
| 92 | push @anchors, [ $anchor, $index ] if defined $anchor && length $anchor; | ||
| 93 | $index_of{ refaddr $widget } = $index; | ||
| 94 | return $widget; | ||
| 95 | } | ||
| 96 | |||
| 97 | method _checked_section_option ($section) { | ||
| 98 | die "Term::Fabulous::Document: section must be [ level, title ], got " . describe($section) unless ref $section eq 'ARRAY' && @$section == 2; | ||
| 99 | my ( $level, $title ) = @$section; | ||
| 100 | die "Term::Fabulous::Document: a section level is an integer from 1 to 6, got " . describe($level) unless defined $level && !ref $level && $level =~ /\A[1-6]\z/; | ||
| 101 | return [ $level + 0, string( $self, 'section title', $title ) ]; | ||
| 102 | } | ||
| 103 | |||
| 104 | method items () { | ||
| 105 | return @items; | ||
| 106 | } | ||
| 107 | |||
| 108 | method count () { | ||
| 109 | return scalar @items; | ||
| 110 | } | ||
| 111 | |||
| 112 | method item ($index) { | ||
| 113 | return $items[ $self->_checked_index( item => $index ) ]; | ||
| 114 | } | ||
| 115 | |||
| 116 | method options_of ($index) { | ||
| 117 | return { %{ $options[ $self->_checked_index( options_of => $index ) ] } }; | ||
| 118 | } | ||
| 119 | |||
| 120 | # What made the item: the name of the factory, or widget for an item | ||
| 121 | # given to add. | ||
| 122 | method kind_of ($index) { | ||
| 123 | return $facts[ $self->_checked_index( kind_of => $index ) ]{kind}; | ||
| 124 | } | ||
| 125 | |||
| 126 | method index_of ($widget) { | ||
| 127 | return blessed $widget ? $index_of{ refaddr $widget } : undef; | ||
| 128 | } | ||
| 129 | |||
| 130 | method code_blocks () { | ||
| 131 | return @code_blocks; | ||
| 132 | } | ||
| 133 | |||
| 134 | # The index of an item of the document, or a death that says why not. | ||
| 135 | method _checked_index ( $method, $index ) { | ||
| 136 | die "Term::Fabulous::Document: $method needs an index, got " . describe($index) unless defined $index && !ref $index && $index =~ /\A[0-9]+\z/; | ||
| 137 | die "Term::Fabulous::Document: item $index does not exist, the document has " . scalar(@items) . " items" unless $index < @items; | ||
| 138 | return $index; | ||
| 139 | } | ||
| 140 | |||
| 141 | # The texts of an item, the item itself first, in tree order: where | ||
| 142 | # its links are. | ||
| 143 | method texts ($index) { | ||
| 144 | my $item = $self->item($index); | ||
| 145 | return grep { $_->isa('Term::Fabulous::Widget::Text') } $item->subtree; | ||
| 146 | } | ||
| 147 | |||
| 148 | method searchables ($index) { | ||
| 149 | return searchables_in( $self->item($index) ); | ||
| 150 | } | ||
| 151 | |||
| 152 | # --------------------------------------------------------------------- | ||
| 153 | # The view that shows the document | ||
| 154 | # --------------------------------------------------------------------- | ||
| 155 | |||
| 156 | method shown_by () { | ||
| 157 | return $view; | ||
| 158 | } | ||
| 159 | |||
| 160 | # For Term::Fabulous::Widget::DocumentView: the items are children of | ||
| 161 | # the slots of one view, so a second view cannot show them too. | ||
| 162 | method _attach_view ($new) { | ||
| 163 | if ( defined $view && refaddr($view) != refaddr($new) ) { | ||
| 164 | my $id = $view->can('id') ? $view->id : undef; | ||
| 165 | die "Term::Fabulous::Document: the document is shown by the " . ref($view) . ( defined $id ? " '$id'" : '' ) . " already; a document is shown by one view at a time"; | ||
| 166 | } | ||
| 167 | $view = $new; | ||
| 168 | weaken $view; | ||
| 169 | return; | ||
| 170 | } | ||
| 171 | |||
| 172 | method _detach_view ($old) { | ||
| 173 | $view = undef if defined $view && refaddr($view) == refaddr($old); | ||
| 174 | return; | ||
| 175 | } | ||
| 176 | |||
| 177 | # --------------------------------------------------------------------- | ||
| 178 | # Factories: the common items, made from text and marks | ||
| 179 | # --------------------------------------------------------------------- | ||
| 180 | |||
| 181 | method heading ( $level, $content, %options ) { | ||
| 182 | die "Term::Fabulous::Document: a heading level is an integer from 1 to 6, got " . describe($level) unless defined $level && !ref $level && $level =~ /\A[1-6]\z/; | ||
| 183 | my $text = $self->_text( heading => $content, $self->_style_of("heading$level") ); | ||
| 184 | my $widget = $level == 1 ? _stack( $text, Term::Fabulous::Widget::Divider->new ) : $text; | ||
| 185 | return $self->_add_made( heading => $widget, 1, flush => 1, section => [ $level, $text->text ], %options ); | ||
| 186 | } | ||
| 187 | |||
| 188 | method paragraph ( $content, %options ) { | ||
| 189 | return $self->_add_made( paragraph => $self->_text( paragraph => $content ), 1, %options ); | ||
| 190 | } | ||
| 191 | |||
| 192 | method term ( $content, %options ) { | ||
| 193 | my $text = $self->_text( term => $content, $self->_style_of('term') ); | ||
| 194 | return $self->_add_made( term => $text, 1, anchor => $text->text, %options ); | ||
| 195 | } | ||
| 196 | |||
| 197 | # A row of two columns: the marker in a column of its own, so that the | ||
| 198 | # lines of the text start after it, a hanging indent done by the | ||
| 199 | # layout; then the text. | ||
| 200 | method list_item ( $marker, $content, %options ) { | ||
| 201 | $marker = string( $self, marker => $marker ); | ||
| 202 | die "Term::Fabulous::Document: an item needs a marker" unless length $marker; | ||
| 203 | my $columns = string_columns($marker); | ||
| 204 | my $width = positive_integer( $self, marker_width => delete $options{marker_width} // $columns + 1 ); | ||
| 205 | my $position = positive_integer( $self, position => delete $options{position} // 1 ); | ||
| 206 | die "Term::Fabulous::Document: marker_width $width leaves no space after the marker '$marker' ($columns columns)" if $width <= $columns; | ||
| 207 | |||
| 208 | my $mark = Term::Fabulous::Document::Marker->new( text => $marker, spans => [ [ 0, length $marker, $self->_style_of('marker') ] ] ); | ||
| 209 | my $column = Term::Fabulous::Widget::Box->new( layout => { sizing => { width => sizing_fixed($width) } } ); | ||
| 210 | $column->add_child($mark); | ||
| 211 | my $row = Term::Fabulous::Widget::Box->new( layout => { layout_direction => CLAY_LEFT_TO_RIGHT, sizing => { width => sizing_grow() } } ); | ||
| 212 | $row->add_child( $column, $self->_text( item => $content ) ); | ||
| 213 | return $self->_add_made( item => $row, $position, %options ); | ||
| 214 | } | ||
| 215 | |||
| 216 | method code ( $code, %options ) { | ||
| 217 | my $language = delete $options{language}; | ||
| 218 | my $block = Term::Fabulous::Widget::CodeBlock->new( | ||
| 219 | code => string( $self, code => $code ), | ||
| 220 | look_class => $look, | ||
| 221 | layout => { sizing => { width => sizing_grow() } }, | ||
| 222 | defined $language ? ( language => $language ) : (), | ||
| 223 | ); | ||
| 224 | push @code_blocks, $block; | ||
| 225 | return $self->_add_made( code => $block, 1, %options ); | ||
| 226 | } | ||
| 227 | |||
| 228 | method rule (%options) { | ||
| 229 | return $self->_add_made( rule => Term::Fabulous::Widget::Divider->new, 1, %options ); | ||
| 230 | } | ||
| 231 | |||
| 232 | sub _stack (@children) { | ||
| 233 | my $box = Term::Fabulous::Widget::Box->new( layout => { layout_direction => CLAY_TOP_TO_BOTTOM, sizing => { width => sizing_grow() } } ); | ||
| 234 | $box->add_child(@children); | ||
| 235 | return $box; | ||
| 236 | } | ||
| 237 | |||
| 238 | # Adds what a factory made, with the blank rows the factory rules give | ||
| 239 | # unless the caller gives them, and records what the next factory needs. | ||
| 240 | method _add_made ( $kind, $widget, $position, %options ) { | ||
| 241 | my $indent = $options{indent} // 0; | ||
| 242 | $options{gap} //= $self->_gap_before( $kind, $indent, $position ); | ||
| 243 | $self->add( $widget, %options ); | ||
| 244 | $facts[-1] = { kind => $kind, indent => $indent, position => $position }; | ||
| 245 | return $widget; | ||
| 246 | } | ||
| 247 | |||
| 248 | # No blank row between the items of a list, nor before the first item | ||
| 249 | # of a list nested in the item above it, nor between a term and its | ||
| 250 | # definition (anything indented deeper than the term); one otherwise. | ||
| 251 | method _gap_before ( $kind, $indent, $position ) { | ||
| 252 | my $previous = $facts[-1] // return 1; | ||
| 253 | return 0 if $kind eq 'item' && $previous->{kind} eq 'item' && ( $position > 1 || $indent > $previous->{indent} ); | ||
| 254 | return 0 if $kind ne 'term' && $previous->{kind} eq 'term' && $indent > $previous->{indent}; | ||
| 255 | return 1; | ||
| 256 | } | ||
| 257 | |||
| 258 | method _style_of ($kind) { | ||
| 259 | return $look->token_style($kind); | ||
| 260 | } | ||
| 261 | |||
| 262 | # A text from a string, { text => ..., marks => [ [ start, end, kind, | ||
| 263 | # link ], ... ] } or { markup => ... }, with the base styles over the | ||
| 264 | # whole text below the styles of its marks. | ||
| 265 | method _text ( $what, $content, @base ) { | ||
| 266 | my ( $text, $spans, $links ); | ||
| 267 | if ( !ref $content ) { | ||
| 268 | ( $text, $spans, $links ) = ( string( $self, $what, $content ), [], [] ); | ||
| 269 | } | ||
| 270 | elsif ( ref $content eq 'HASH' && exists $content->{markup} ) { | ||
| 271 | die "Term::Fabulous::Document: markup cannot be given together with text or marks" if keys %$content > 1; | ||
| 272 | ( $text, $spans, $links ) = parse_markup( string( $self, "$what markup", $content->{markup} ) ); | ||
| 273 | } | ||
| 274 | elsif ( ref $content eq 'HASH' ) { | ||
| 275 | my @unknown = sort grep { $_ ne 'text' && $_ ne 'marks' } keys %$content; | ||
| 276 | die "Term::Fabulous::Document: a $what does not know @unknown (known: text, marks, markup)" if @unknown; | ||
| 277 | $text = string( $self, "$what text", $content->{text} // '' ); | ||
| 278 | ( $spans, $links ) = $self->_spans_and_links( $what, $content->{marks} // [] ); | ||
| 279 | } | ||
| 280 | else { | ||
| 281 | die "Term::Fabulous::Document: a $what needs a string, { text => ..., marks => [...] } or { markup => ... }, got " . describe($content); | ||
| 282 | } | ||
| 283 | my $length = length $text; | ||
| 284 | return Term::Fabulous::Widget::Text->new( text => $text, spans => [ ( map { [ 0, $length, $_ ] } @base ), @$spans ], links => $links ); | ||
| 285 | } | ||
| 286 | |||
| 287 | # Marks as spans in the styles of their kinds, and links. | ||
| 288 | method _spans_and_links ( $what, $marks ) { | ||
| 289 | die "Term::Fabulous::Document: the marks of a $what must be an array reference, got " . describe($marks) unless ref $marks eq 'ARRAY'; | ||
| 290 | my ( @spans, @links ); | ||
| 291 | foreach my $mark (@$marks) { | ||
| 292 | die "Term::Fabulous::Document: a mark is [ start, end, kind ] or [ start, end, 'link', target ], got " . describe($mark) unless ref $mark eq 'ARRAY' && ( @$mark == 3 || @$mark == 4 ); | ||
| 293 | my ( $start, $end, $kind, $target ) = @$mark; | ||
| 294 | if ( defined $kind && !ref $kind && $kind eq 'link' ) { | ||
| 295 | push @links, [ $start, $end, $target ]; | ||
| 296 | next; | ||
| 297 | } | ||
| 298 | die "Term::Fabulous::Document: only a mark of kind link has a target" if @$mark == 4; | ||
| 299 | push @spans, [ $start, $end, $self->_style_of($kind) ]; | ||
| 300 | } | ||
| 301 | return ( \@spans, \@links ); | ||
| 302 | } | ||
| 303 | |||
| 304 | # --------------------------------------------------------------------- | ||
| 305 | # Sections | ||
| 306 | # --------------------------------------------------------------------- | ||
| 307 | |||
| 308 | method sections () { | ||
| 309 | return map { { item => $_->[0], level => $_->[1], title => $_->[2] } } @sections; | ||
| 310 | } | ||
| 311 | |||
| 312 | method section_count () { | ||
| 313 | return scalar @sections; | ||
| 314 | } | ||
| 315 | |||
| 316 | # A section number, as section_at returns it, or a death that says why | ||
| 317 | # not. | ||
| 318 | method _checked_section ( $method, $number ) { | ||
| 319 | die "Term::Fabulous::Document: $method needs a section number, got " . describe($number) unless defined $number && !ref $number && $number =~ /\A[0-9]+\z/; | ||
| 320 | die "Term::Fabulous::Document: section $number does not exist, the document has " . scalar(@sections) . " sections" unless $number < @sections; | ||
| 321 | return $number; | ||
| 322 | } | ||
| 323 | |||
| 324 | method item_of_section ($number) { | ||
| 325 | return $sections[ $self->_checked_section( item_of_section => $number ) ][0]; | ||
| 326 | } | ||
| 327 | |||
| 328 | method level_of_section ($number) { | ||
| 329 | return $sections[ $self->_checked_section( level_of_section => $number ) ][1]; | ||
| 330 | } | ||
| 331 | |||
| 332 | method title_of_section ($number) { | ||
| 333 | return $sections[ $self->_checked_section( title_of_section => $number ) ][2]; | ||
| 334 | } | ||
| 335 | |||
| 336 | # The number of the section the item is in: the last section that | ||
| 337 | # starts at or before it. undef for items before the first section. | ||
| 338 | method section_at ($index) { | ||
| 339 | $index = $self->_checked_index( section_at => $index ); | ||
| 340 | my ( $low, $high ) = ( 0, $#sections ); | ||
| 341 | return undef if $high < 0 || $sections[0][0] > $index; | ||
| 342 | while ( $low < $high ) { | ||
| 343 | my $middle = int( ( $low + $high + 1 ) / 2 ); | ||
| 344 | if ( $sections[$middle][0] <= $index ) { $low = $middle } | ||
| 345 | else { $high = $middle - 1 } | ||
| 346 | } | ||
| 347 | return $low; | ||
| 348 | } | ||
| 349 | |||
| 350 | sub _normalized ($text) { | ||
| 351 | return join ' ', split ' ', $text; | ||
| 352 | } | ||
| 353 | |||
| 354 | # The item a link to the section leads to. Sections come before | ||
| 355 | # anchors, exact matches before matches that ignore case, and those | ||
| 356 | # before a title or anchor that starts with the name as a word | ||
| 357 | # ("open" finds "open FILEHANDLE,EXPR" in perlfunc). | ||
| 358 | method find_section ($name) { | ||
| 359 | die "Term::Fabulous::Document: find_section needs a section name, got " . describe($name) unless defined $name && !ref $name; | ||
| 360 | my $wanted = _normalized($name); | ||
| 361 | return undef unless length $wanted; | ||
| 362 | |||
| 363 | my @targets = ( ( map { [ _normalized( $_->[2] ), $_->[0] ] } @sections ), @anchors ); | ||
| 364 | my @rules = ( | ||
| 365 | sub ($text) { $text eq $wanted }, | ||
| 366 | sub ($text) { fc($text) eq fc($wanted) }, | ||
| 367 | sub ($text) { $text =~ /\A\Q$wanted\E(?!\w)/ }, | ||
| 368 | sub ($text) { $text =~ /\A\Q$wanted\E(?!\w)/i }, | ||
| 369 | ); | ||
| 370 | foreach my $rule (@rules) { | ||
| 371 | my $found = first { $rule->( $_->[0] ) } @targets; | ||
| 372 | return $found->[1] if defined $found; | ||
| 373 | } | ||
| 374 | return undef; | ||
| 375 | } | ||
| 376 | |||
| 377 | # --------------------------------------------------------------------- | ||
| 378 | # Searching | ||
| 379 | # --------------------------------------------------------------------- | ||
| 380 | |||
| 381 | # Every occurrence of the text in the texts the searchable widgets of | ||
| 382 | # the items show (Term::Fabulous::Role::Searchable), as [ item index, | ||
| 383 | # widget, key, start, end ], in the order of the document. A query | ||
| 384 | # without capital letters ignores case. | ||
| 385 | method find_text ($query) { | ||
| 386 | die "Term::Fabulous::Document: find_text needs a string, got " . describe($query) unless defined $query && !ref $query; | ||
| 387 | return () unless length $query; | ||
| 388 | my $pattern = $query =~ /\p{Lu}/ ? qr/\Q$query\E/ : qr/\Q$query\E/i; | ||
| 389 | |||
| 390 | my @matches; | ||
| 391 | foreach my $index ( 0 .. $#items ) { | ||
| 392 | foreach my $widget ( searchables_in( $items[$index] ) ) { | ||
| 393 | foreach my $entry ( $widget->search_texts ) { | ||
| 394 | my ( $key, $text ) = @$entry; | ||
| 395 | push @matches, [ $index, $widget, $key, $-[0], $+[0] ] while $text =~ /$pattern/g; | ||
| 396 | } | ||
| 397 | } | ||
| 398 | } | ||
| 399 | return @matches; | ||
| 400 | } | ||
| 401 | } | ||
| 402 | |||
| 403 | 1; | ||
| 404 | |||
| 405 | __END__ | ||