Created
April 24, 2009 22:16
-
-
Save phillipadsmith/101376 to your computer and use it in GitHub Desktop.
rich_paragraph.mc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% $paragraph %> | |
<%INIT> | |
my %embeds = (); | |
# Get paragraph with markup language | |
my $markup = $element->get_data('paragraph'); | |
# Store embedded elements in a hash | |
foreach my $e ($element->get_elements) { | |
if ($e->has_name('embed_related_image')) { | |
my $key = "image[" . $e->get_data('key') . "]"; | |
my $uri = $e->get_related_media->get_uri; | |
my $title = $e->get_data('alt') || $e->get_related_media->get_title; | |
my $size = $e->get_data('size'); | |
my $class = $e->get_data('class'); | |
# replace image if size not original | |
if ($size ne 'original') { | |
$uri =~ s/(\.\w*)$/$size$1/; | |
} | |
$embeds{$key} = [$uri, $title, $class]; | |
} | |
elsif ($e->has_name('embed_related_story')) { | |
my $story = $e->get_related_story; | |
return $m->comp('PARENT:missingElementError', key=>'story', type=>'story') unless $story; | |
my $key = 'story[' . $e->get_data('key') . ']'; | |
my $uri = $story->get_uri; | |
my $title = $story->get_title; | |
my $anchor = $e->get_data('anchor'); | |
$embeds{$key} = [$uri, $title, $anchor]; | |
} | |
elsif ($e->has_name('embed_related_url')) { | |
my $story = $e->get_related_story; | |
return $m->comp('PARENT:missingElementError', key=>'url', type=>'story') unless $story; | |
my $key = 'url[' . $e->get_data('key') . ']'; | |
my $uri = $story->get_data('protocol').$story->get_data('url'); | |
my $title = $story->get_title; | |
my $properties = $story->get_container('dataobjects_properties_en'); | |
if ($properties) { | |
$title = $properties->get_data('name') || $properties->get_data('heading'); | |
} | |
$embeds{$key} = [$uri, $title]; | |
} | |
} | |
while (1) { | |
if ($markup =~ /(?:"([^"]+)":)?((story|image|url)\[[^[\]]+\])/) { | |
my ($title, $key, $type, $start, $end) = ($1, $2, $3, $-[0], $+[0]); | |
my $data = $embeds{$key}; | |
next unless $data; | |
if ($type eq 'image') { | |
my $uri = $data->[0]; | |
$title = $title || $data->[1]; | |
my $class = $data->[2]; | |
my $tag = '!'; | |
$tag .= "($class)" if $class ne 'none'; | |
$tag .= $uri; | |
$tag .= "($title)" if $title ne ''; | |
$tag .= "!"; | |
$markup = $` . $tag . $'; | |
# substr($markup, $start, $end, $tag); | |
} | |
elsif ($type eq 'story') { | |
my $uri = $data->[0]; | |
$title = $title || $data->[1]; | |
my $anchor = $data->[2]; | |
my $tag = '"'.$title.'":'.$uri; | |
$tag .= '#'.$anchor if $anchor; | |
$markup = $`.$tag.$'; | |
#substr($markup, $start, $end, $tag); | |
} | |
elsif ($type eq 'url') { | |
my $uri = $data->[0]; | |
$title = $title || $data->[1] || $data->[0]; | |
my $tag = '"'.$title.'":'.$uri; | |
$markup = $`.$tag.$'; | |
# substr($markup, $start, $end, $tag); | |
} | |
} | |
else { | |
last; | |
} | |
} | |
my $textile = new Text::Textile; | |
my $paragraph = $textile->process($markup); | |
</%INIT> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment