Added some shady media tag processing. Anchor style, [[my_anchor]] can have [[image:my_image.png]], [[audio:...]], [[video:...]], [[youtube:...]], and [[vimeo:...]]. For the HTML converter, this creates img, audio, video, and iframes respectively. I should add an .href modify that encases the given tags in an anchor. Additionally, elements should likely have a default modifier where you are not required to explicitly mark the formatter. Example: [[my_anchor]].href(url) could just be [[my_anchor]](url).

master
kts 2014-12-16 16:00:21 -08:00
parent 80245d57f1
commit 72761033d0
6 changed files with 189 additions and 8 deletions

View File

@ -3,6 +3,7 @@
kettext...
````````````````````````````````
* **is a markup language**
* **is a perl module and script**
* **is for the design-inclined programmer**
* **is easy to write with**
* **looks nice in a terminal**

View File

@ -341,6 +341,24 @@ sub HTML {
my @elements = @{$data{elements}};
my %settings = %{$data{settings}};
my $file_name = $data{filename};
our @prefixes = (["img", "image"], ["aud", "audio"], ["vid", "video"], ["yt", "youtube"], ["vimeo"], ["twitter", "tweet"]);
our @urls = ([], [], [], ["youtube.com", "youtu.be"], ["vimeo.com"], ["twitter.com"]);
our @url_parse = (
"(.*)",
"",
"",
"(?:.*)(?:v=)([a-zA-Z0-9]*)",
"(?:.*)(?:/)([0-9)*)\$",
"");
our @embeds = (
['"<img src=\"$1\">"',''],
['"<audio src=\"$1\" controls>"','</audio>'],
['"<video src=\"$1\" controls>"','</video>'],
['"<iframe src=\"//www.youtube.com/embed/$1\" frameborder=\"0\" allowfullscreen>"', '</iframe>'],
['"<iframe src=\"//player.vimeo.com/video/$1\" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen>"', '</iframe>'],
["", ""]);
# print 'em out
if ($settings{'toc'}) {
foreach (@elements) {
@ -409,28 +427,97 @@ sub HTML {
}
sub convertText {
my $text = $_[0];
my @text_patterns = ('\*\*','\*\*', '\/\/','\/\/', '\[\[','\]\]', '\(\(','\)\)', '``', '``');
my @text_replace = ('<b>','</b>', '<i>','</i>', '<a>','</a>', '<small>','</small>', '<code>','</code>');
my @text_patterns = ('\*\*','\*\*', '\/\/','\/\/', '\[\[','\]\]', '\(\(','\)\)', '``', '``', '__','__');
my @text_replace = ('<b>','</b>', '<i>','</i>', '<a>','</a>', '<small>','</small>', '<code>','</code>', '<u>','</u>');
my @char_patterns = ('&', '<', '>', '"', '\'', '--');
my @char_replace = ('&amp;', '&lt;', '&gt;', '&quot;', '&apos;', '&mdash;');
my $i = 0;
my $new_text = $text;
# 1. replace character(s)
#for (my $ch = 0; $ch <= $#char_patterns; $ch++) {
# if ($new_text =~ m/(([^\\])$char_patterns[$ch])/g) {
# my $bzr = $2;
# $new_text =~ s/\Q$bzr$char_patterns[$ch]\E/\Q$bzr\E$char_replace[$ch]/g;
# }
# if ($new_text =~ m/(\\)($char_patterns[$ch])/g) {
# $new_text =~ s/\\$char_patterns[$ch]/$char_patterns[$ch]/g;
# }
#}
# 2. replace character pairs
for (my $p = 0; $p <= $#text_patterns; $p+=2) {
my $open = $text_patterns[$p];
my $close = $text_patterns[$p+1];
while ($new_text =~ m/($open)([^$close]*)($close)(\.\S*\(.*?\)|)/g) {
#while ($new_text =~ m/($open)([^$close]*)($close)(\.\S*\(.*\)|)/g) {
my $converted_text;
if ($4) {
my $ts = substr($text_replace[$i], 0, -1);
my $te = substr($text_replace[$i], -1);
$converted_text = $ts.kettext::parseOptions($4).$te.$2.$text_replace[$i+1];
if ($p == 4) {
my @media = convertLink($2);
if ($4) {
my $ts = substr($media[0], 0, -1);
my $te = substr($media[0], -1);
$converted_text = $ts.kettext::parseOptions($4).$te.$media[1];
} else {
$converted_text = $media[0].$media[1];
}
$new_text =~ s/\Q$1$2$3$4\E/$converted_text/g;
} else {
$converted_text = $text_replace[$i].$2.$text_replace[$i+1];
if ($4) {
my $ts = substr($text_replace[$i], 0, -1);
my $te = substr($text_replace[$i], -1);
$converted_text = $ts.kettext::parseOptions($4).$te.$2.$text_replace[$i+1];
} else {
$converted_text = $text_replace[$i].$2.$text_replace[$i+1];
}
$new_text =~ s/\Q$1$2$3$4\E/$converted_text/g;
}
$new_text =~ s/\Q$1$2$3$4\E/$converted_text/g;
}
$i +=2;
}
return $new_text;
}
sub convertLink {
my $text = $_[0];
my $find = $text;
# 1. check for prefix such as "image:", "audio:", etc.
if ($find =~ m/^(.*?)(?::)(.*)$/) {
for (my $prefix_group = 0; $prefix_group <= $#prefixes; $prefix_group++) {
for (my $prefix = 0; $prefix <= $#{${prefixes[$prefix_group]}}; $prefix++) {
if ($1 eq ${prefixes[$prefix_group]}[$prefix]) {
my @media = urlToMedia($2); #"prefix:http://..."
if ($media[0] eq "") { # "prefix:data"
$media[0] = $2;
$media[0] =~ s/(.*)/$embeds[$prefix_group][0]/ee;
$media[1] = $embeds[$prefix_group][1];
}
return @media;
}
}
}
}
# 2. check for (http(s)://)(www.)youtube/youtu.be/vimeo/etc.
my @media = urlToMedia($text);
if (!$media[0] eq "") { #"http://..."
return @media;
}
# 3. Finally, check file extension
# 4. Just an anchor
return ("<a>","$text</a>");
}
sub urlToMedia {
my $find = $_[0];
if ($find =~ m/^(?:http(?:s|):\/\/(?:www.|)|)(.*)\/(.*)/) {
for (my $urls_i = 0; $urls_i <= $#urls; $urls_i++) {
for (my $url_i = 0; $url_i <= $#{${urls[$urls_i]}}; $url_i++) {
if ($1 eq ${urls[$urls_i]}[$url_i]) {
my $match = $2;
$match =~ s/$url_parse[$urls_i]/$embeds[$urls_i][0]/ee; # the ee is qqing
return ($match,$embeds[$urls_i][1]);
}
}
}
}
return ("","");
}
}
1;

24
test/chars.ktx 100644
View File

@ -0,0 +1,24 @@
HTML conversions:
Signs: < > < > < > < >
Quote: " " " "
Apostrophe: ' ' ' '
Ampersand: & & & &
Em-dash: -- -- -- --
----
Escaped:
Signs: \< \> \< \> \< \> \< \>
Quote: \" \" \" \"
Apostrophe: \' \' \' \'
Ampersand: \& \& \& \&
Em-dash: \-- \-- \-- \--
----
Mixed:
Signs: \< \> < > \< \> < >
Quote: \" " \" "
Apostrophe: \' ' \' '
Ampersand: \& & \& &
Em-dash: \-- -- \-- --

3
test/include.ktx 100644
View File

@ -0,0 +1,3 @@
This file includes "spacer.ktx"
.include(spacer.ktx).imply(header.depth=1,version.hide)

59
test/media.ktx 100644
View File

@ -0,0 +1,59 @@
Media syntax uses the anchor-style as the encapsulator for the media. This was chosen due to the logical similarity of a link relating to an external location.
* images
[[image:my_image.png]]
#[[image:my_image.png]]
#<img src="my_image.png">
Audio and video rely on HTML5 video support.
* audio
[[audio:my_audio.ogg]]
#[[audio:my_audio.ogg]]
#<audio src="audio.ogg" controls autoplay loop>
* multiple audio sources (not implemented)
[[my_audio.ogg, my_audio.mp3]]
[[audio:my_audio.ogg, my_audio.mp3]]
#<audio controls>
# <source src="my_audio.ogg" type="audio/ogg">
# <source src="my_audio.mp3" type="audio/mp3">
# Your browser does not support the <code>audio</code> element.
#</audio>
* video
[[video:my_video.ogm]]
#[[video:my_video.ogm]]
#<video src="my_video.ogm" controls>
#Your browser does not support the <code>video</code> element.
#</video>
* multiple video sources (not implemented)
[[my_video.ogm,my_video.mp4]]
[[video:my_video.ogm, my_video.mp4]]
#<video controls>
# <source src="my_video.ogm" type="video/ogg">
# <source src="my_video.mp4" type="video/mp4">
# Your browser does not support the <code>video</code> element.
#</video>
Support also exists for embedded videos from youtube and vimeo.
* youtube
[[youtube:aMp9nZ57gqI]]
#[[youtube:aMp9nZ57gqI]]
#[[youtube:http://youtube.com/watch?v=aMp9nZ57gqI]]
#[[http://youtube.com/watch?v=aMp9nZ57gqI]]
#<iframe width="560" height="315" src="//www.youtube.com/embed/aMp9nZ57gqI" frameborder="0" allowfullscreen></iframe>
* vimeo
[[vimeo:1084537]]
#[[vimeo:1084537]]
#[[vimeo:http://vimeo.com/1084537]]
#[[http://vimeo.com/1084537]]
#<iframe src="//player.vimeo.com/video/1084537" width="500" height="281" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>

View File

@ -0,0 +1,7 @@
[[youtube:aMp9nZ57gqI]]
[[youtube:http://youtube.com/watch?v=aMp9nZ57gqI]]
[[http://youtube.com/watch?v=aMp9nZ57gqI]]
[[img:my_image.png]]
[[my link]].href(lol)