]> wagner.pp.ru Git - oss/vjournal.git/commitdiff
Initial commit
authorVictor Wagner <vitus@wagner.pp.ru>
Tue, 8 Oct 2013 04:09:24 +0000 (08:09 +0400)
committerVictor Wagner <vitus@wagner.pp.ru>
Tue, 8 Oct 2013 04:09:24 +0000 (08:09 +0400)
MANIFEST [new file with mode: 0644]
Makefile.PL [new file with mode: 0644]
lib/VJournal/Format.pm [new file with mode: 0644]
t/user_link.t [new file with mode: 0644]

diff --git a/MANIFEST b/MANIFEST
new file mode 100644 (file)
index 0000000..31fd4b4
--- /dev/null
+++ b/MANIFEST
@@ -0,0 +1,4 @@
+Makefile.PL
+MANIFEST
+lib/VJournal/Format.pm
+t/user_link.t
diff --git a/Makefile.PL b/Makefile.PL
new file mode 100644 (file)
index 0000000..4cebc97
--- /dev/null
@@ -0,0 +1,9 @@
+#!/usr/bin/perl
+
+use ExtUtils::MakeMaker;
+
+WriteMakefile(
+       NAME=>'MicroBlog',
+       VERSION=>0.1
+);
+
diff --git a/lib/VJournal/Format.pm b/lib/VJournal/Format.pm
new file mode 100644 (file)
index 0000000..9dbecf1
--- /dev/null
@@ -0,0 +1,35 @@
+package VJournal::Format;
+use strict;
+use vars qw(@EXPORT);
+@EXPORT=qw(format_post format_tape format_comment);
+
+our $imgprefix="/img";
+
+our %sites=(
+lj=>{uri=>'http://%s.livejournal.com',convertunderscore=>1,image=>'lj.gif'},
+ljr=>{uri=>'http://lj.rossia.org/users/%s',convertunderscore=>0,image=>'ljr.gif'},
+dw=>{uri=>'http://%s.dreamwidth.org',convertunderscore=>1,image=>'dw.gif'},
+);
+
+sub user_link {
+       my ($site,$name,$title) = @_;
+       $title ||=$name;
+       return "" unless exists $sites{$site};
+       my $s=$sites{$site};
+       if ($s->{convertunderscore}) {
+               $name=~tr/_/-/;
+       }
+       my $uri=sprintf($s->{uri},$name);
+       return "<a href=\"$uri\"><img src=\"$imgprefix/".$s->{image}."\"><b>$title</b></a>";
+}
+
+sub find_user_links {
+       local $_ = pop @_;
+       my $expr="<(".join("|",keys %sites).')\s+user="?(\w+)"?(?:\s+title="([^\"]+)")?\s*/?>';
+       s/$expr/user_link($1,$2,$3)/seg;
+       return $_;
+}
+sub find_http_links {
+
+}
+
diff --git a/t/user_link.t b/t/user_link.t
new file mode 100644 (file)
index 0000000..9dbce7f
--- /dev/null
@@ -0,0 +1,98 @@
+#!/usr/bin/perl
+
+use Test::More tests => 23 ;
+BEGIN {use_ok('VJournal::Format');}
+
+=head1 Low-level link formatting
+
+Check link formatting for known sites. Use cases simple name, name with
+underscore and alternate title specifed.
+
+=head2 Livejournal
+
+=cut
+
+is(MicroBlog::Format::user_link("lj","besm6"),'<a href="http://besm6.livejournal.com"><img src="/img/lj.gif"><b>besm6</b></a>','LJ simple');
+is(MicroBlog::Format::user_link("lj","vitus_wagner"),'<a href="http://vitus-wagner.livejournal.com"><img src="/img/lj.gif"><b>vitus_wagner</b></a>','LJ underscore');
+is(MicroBlog::Format::user_link("lj","besm6","Artem"),'<a href="http://besm6.livejournal.com"><img src="/img/lj.gif"><b>Artem</b></a>','LJ title');
+
+=head2 Dreamwidth
+
+=cut
+
+is(MicroBlog::Format::user_link("dw","besm6"),'<a href="http://besm6.dreamwidth.org"><img src="/img/dw.gif"><b>besm6</b></a>','DW simple');
+is(MicroBlog::Format::user_link("dw","vitus_wagner"),'<a href="http://vitus-wagner.dreamwidth.org"><img src="/img/dw.gif"><b>vitus_wagner</b></a>','DW underscore');
+is(MicroBlog::Format::user_link("dw","besm6","Artem"),'<a href="http://besm6.dreamwidth.org"><img src="/img/dw.gif"><b>Artem</b></a>','DW title');
+
+=head2 lj.rossia.org
+
+=cut
+
+is(MicroBlog::Format::user_link("ljr","kouzdra"),'<a href="http://lj.rossia.org/users/kouzdra"><img src="/img/ljr.gif"><b>kouzdra</b></a>','LJR simple');
+is(MicroBlog::Format::user_link("ljr","some_user"),'<a href="http://lj.rossia.org/users/some_user"><img src="/img/ljr.gif"><b>some_user</b></a>','LJ underscore');
+is(MicroBlog::Format::user_link("ljr","kouzdra","Anton"),'<a href="http://lj.rossia.org/users/kouzdra"><img src="/img/ljr.gif"><b>Anton</b></a>','LJ title');
+
+=head1 html parsing
+
+=head2 simple cases for all sites
+
+=cut
+
+is(MicroBlog::Format::find_user_links('<lj user="filin">'),
+   MicroBlog::Format::user_link('lj','filin'),"Parse LJ");
+
+   
+is(MicroBlog::Format::find_user_links('<dw user="filin">'),
+   MicroBlog::Format::user_link('dw','filin'),"Parse DW");
+
+is(MicroBlog::Format::find_user_links('<ljr user="filin">'),
+   MicroBlog::Format::user_link('ljr','filin'),"Parse LJR");
+
+=head2 no quotes
+
+=cut
+
+is(MicroBlog::Format::find_user_links('<lj user=filin>'),
+   MicroBlog::Format::user_link('lj','filin'),"No quotes");
+
+=head2 various spaces
+
+=cut
+
+is(MicroBlog::Format::find_user_links('<lj user="filin" >'),
+   MicroBlog::Format::user_link('lj','filin'),"Space before closing tag");
+is(MicroBlog::Format::find_user_links('<lj  user="filin">'),
+   MicroBlog::Format::user_link('lj','filin'),"Two spaces");
+is(MicroBlog::Format::find_user_links('<lj user="filin" />'),
+   MicroBlog::Format::user_link('lj','filin'),"XML empty tag");
+is(MicroBlog::Format::find_user_links("<lj\tuser=filin>"),
+   MicroBlog::Format::user_link('lj','filin'),"tab instead of space");
+
+=head2 title
+
+=cut
+
+is(MicroBlog::Format::find_user_links("<lj user=filin title=\"Artem\">"),
+   MicroBlog::Format::user_link('lj','filin','Artem'),"title");
+
+is(MicroBlog::Format::find_user_links("<lj user=filin title=\"Other guy\">"),
+   MicroBlog::Format::user_link('lj','filin','Other guy'),"Title with space");
+
+is(MicroBlog::Format::find_user_links("<lj\nuser=filin\ntitle=\"Other guy\">"),
+   MicroBlog::Format::user_link('lj','filin','Other guy'),"Multiline");
+
+
+=head2 Multiple user names   
+
+=cut
+
+
+is(MicroBlog::Format::find_user_links('<lj user="filin"> and <lj user="kilka">'),
+   MicroBlog::Format::user_link('lj','filin')." and ".
+   MicroBlog::Format::user_link('lj','kilka')
+   ,"Two user links on same line");
+
+is(MicroBlog::Format::find_user_links("<lj user=\"filin\">\n\n<lj user=\"kilka\">"),
+MicroBlog::Format::user_link('lj','filin')."\n\n".
+   MicroBlog::Format::user_link('lj','kilka')
+   ,"Two user links on separate lines");