Text-Restructured

 view release on metacpan or  search on metacpan

lib/Text/Restructured.pm  view on Meta::CPAN

sub admonition {
    my($parser, $name, $parent, $source, $lineno, $dtext, $lit) = @_;

    my $dhash = parse_directive($parser, $dtext, $lit, $source, $lineno);
    return $dhash if ref($dhash) =~ /$DOM$/o;
    my $content = $dhash->{content} ne '' ? $dhash->{content} :
	$dhash->{args};
    return $parser->system_message
	(3, $source, $lineno,
	 qq(The "$name" admonition is empty; content required.), $lit)
	if $content =~ /^$/;
    my $adm = $DOM->new(lc $name);
    if ($name eq 'admonition') {
	# A generic admonition
	my $ttext = $dhash->{args};
	my $err = arg_check($parser, $name, $source, $lineno, $ttext, $lit,
			    '1+');
	return $err if $err;
	$adm->{attr}{classes} = [ $dhash->{options}{class} ||
				  $parser->NormalizeId("$name-$ttext") ];
	my $title = $DOM->new('title');
	$title->append($DOM->newPCDATA($ttext));
	$adm->append($title);
    }
    $parser->Paragraphs($adm, $content, $source, $dhash->{content_lineno});

    return $adm;
}

# Built-in handler for ascii-mathml directives.
# Arguments: parser, directive name, parent, source, line number,
#            directive text, literal text
# Returns: array of DOM objects
sub ascii_mathml {
    my($parser, $name, $parent, $source, $lineno, $dtext, $lit) = @_;
    my @optlist = qw(label mstyle);
    my $dhash = parse_directive($parser, $dtext, $lit, $source, $lineno,
				\@optlist);
    return $dhash if ref($dhash) =~ /$DOM$/o;
    my($args, $options, $content) = map($dhash->{$_},
					qw(args options content));
    foreach my $optname (qw(mstyle)) {
	my $opt = $options->{$optname};
	next unless defined $opt;
	my $err = check_fieldlist_option($parser, $name, $optname, $opt,
					 $opt, $source, $lineno, $lit);
	return $err if $err;
    }
    return Text::Restructured::Directive::system_msg
	($parser, $name, 3, $source, $lineno,
	 qq(Cannot have both argument and content.), $lit)
	if $args !~ /^$/ && $content !~ /^$/;

    my $subst = $parent->tag eq 'substitution_definition';
    my $text = "$args$content";
    if (! $parser->{_MathML}) {
	eval "use Text::ASCIIMathML";
	$parser->{_MathML} = new Text::ASCIIMathML unless $@;
    }
    chomp $text;
    my $pcdata = $DOM->newPCDATA("$text\n");
    my $math = $parser->{_MathML} ? $DOM->new('mathml') : $pcdata;
    my $label_sub;
    if ($parser->{_MathML}) {
	my %mstyle = (($subst ? () : (displaystyle=>'true')),
			($options->{mstyle} ?
			 %{$parser->HashifyFieldList($options->{mstyle})} :
			 ()),
			);
	my @mstyle_attr = map(($_, $mstyle{$_}), sort keys %mstyle);
	my $label = $options->{label};
	$math->{attr}{mathml} = $parser->{_MathML}->TextToMathMLTree
	    ($text, [title=>$text, xmlns=>"&mathml;",
		     $label ? (label=>$label) : ()],
	     [($parser->{opt}{D}{mstyle} ?
	       @{$parser->{opt}{D}{mstyle}} : ()), @mstyle_attr]);
	return if ! defined $math->{attr}{mathml};
	$math->append($pcdata);
	if ($label) {
	    $label_sub = 
		$DOM->new('substitution_definition',
			  names=>[$parser->NormalizeName($label, 'keepcase')]);
	    my $counter = ++$parser->{TOPDOM}{equation};
	    my $pcdata = $DOM->newPCDATA($counter);
	    my $err = $parser->RegisterName($label_sub, $source, $lineno);
	    $label_sub->append($pcdata);
	    $math->{attr}{label} = $counter;
	}
    }
    return $math if $subst;

    my $para = $DOM->new('paragraph');
    $para->append($math);
    $para->append($label_sub) if $label_sub;
    return $para;
}

# Built-in handler for class directives.
# Arguments: parser obj, directive name, parent, source, line number,
#            directive text, literal text
# Returns: array of DOM objects
sub class {
    my($parser, $name, $parent, $source, $lineno, $dtext, $lit) = @_;
    my @optlist = qw(parent);
    my $dhash = parse_directive($parser, $dtext, $lit, $source, $lineno,
				\@optlist, '1+');
    return $dhash if ref($dhash) =~ /$DOM$/o;

    return $parser->system_message
	(3, $source, $lineno,
	 qq(The "$name" directive may not have contents.), $lit)
	if $dhash->{content} ne '';

    my($args, $options) = map($dhash->{$_}, qw(args options));
    return $parser->system_message
	(3, $source, $lineno,
	 qq(Invalid class attribute value for "$name" directive: "$args".),
	 $lit)
	unless $args =~ /^[a-z][-a-z0-9]*(?:\s+[a-z][-a-z0-9]*)*$/i;

    my $pending = $DOM->new('pending');
    $pending->{internal}{'.transform'} = "docutils.transforms.parts.Class";
    my $details = $pending->{internal}{'.details'} = { };
    @{$details}{qw(class parent)} = ($args, $options->{parent});
    @{$pending}{qw(source lineno lit)} = ($source, $lineno, $lit);
    return $pending;
}

# Built-in handler for compound directive.
# Arguments: parser obj, directive name, parent, source, line number,
#            directive text, literal text
# Returns: array of DOM objects
sub compound {
    my($parser, $name, $parent, $source, $lineno, $dtext, $lit) = @_;
    my @optlist = qw(class);
    my $dhash = parse_directive($parser, $dtext, $lit, $source, $lineno,
				\@optlist);
    return $dhash if ref($dhash) =~ /$DOM$/o;
    my($content, $args, $options) =
	map($dhash->{$_}, qw(content args options));
    return system_msg
	($parser, $name, 3, $source, $lineno,
	 'no arguments permitted; blank line required before content block.',
	 $lit)
	if $content ne '' && $args ne '';



( run in 1.229 second using v1.01-cache-2.11-cpan-2398b32b56e )