From: Victor Wagner Date: Tue, 8 Oct 2013 04:09:24 +0000 (+0400) Subject: Initial commit X-Git-Url: http://wagner.pp.ru/gitweb/?a=commitdiff_plain;h=3774c22b0082af323323dc26eede488611c6ee35;p=oss%2Fvjournal.git Initial commit --- 3774c22b0082af323323dc26eede488611c6ee35 diff --git a/MANIFEST b/MANIFEST new file mode 100644 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 index 0000000..4cebc97 --- /dev/null +++ b/Makefile.PL @@ -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 index 0000000..9dbecf1 --- /dev/null +++ b/lib/VJournal/Format.pm @@ -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 "{image}."\">$title"; +} + +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 index 0000000..9dbce7f --- /dev/null +++ b/t/user_link.t @@ -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"),'besm6','LJ simple'); +is(MicroBlog::Format::user_link("lj","vitus_wagner"),'vitus_wagner','LJ underscore'); +is(MicroBlog::Format::user_link("lj","besm6","Artem"),'Artem','LJ title'); + +=head2 Dreamwidth + +=cut + +is(MicroBlog::Format::user_link("dw","besm6"),'besm6','DW simple'); +is(MicroBlog::Format::user_link("dw","vitus_wagner"),'vitus_wagner','DW underscore'); +is(MicroBlog::Format::user_link("dw","besm6","Artem"),'Artem','DW title'); + +=head2 lj.rossia.org + +=cut + +is(MicroBlog::Format::user_link("ljr","kouzdra"),'kouzdra','LJR simple'); +is(MicroBlog::Format::user_link("ljr","some_user"),'some_user','LJ underscore'); +is(MicroBlog::Format::user_link("ljr","kouzdra","Anton"),'Anton','LJ title'); + +=head1 html parsing + +=head2 simple cases for all sites + +=cut + +is(MicroBlog::Format::find_user_links(''), + MicroBlog::Format::user_link('lj','filin'),"Parse LJ"); + + +is(MicroBlog::Format::find_user_links(''), + MicroBlog::Format::user_link('dw','filin'),"Parse DW"); + +is(MicroBlog::Format::find_user_links(''), + MicroBlog::Format::user_link('ljr','filin'),"Parse LJR"); + +=head2 no quotes + +=cut + +is(MicroBlog::Format::find_user_links(''), + MicroBlog::Format::user_link('lj','filin'),"No quotes"); + +=head2 various spaces + +=cut + +is(MicroBlog::Format::find_user_links(''), + MicroBlog::Format::user_link('lj','filin'),"Space before closing tag"); +is(MicroBlog::Format::find_user_links(''), + MicroBlog::Format::user_link('lj','filin'),"Two spaces"); +is(MicroBlog::Format::find_user_links(''), + MicroBlog::Format::user_link('lj','filin'),"XML empty tag"); +is(MicroBlog::Format::find_user_links(""), + MicroBlog::Format::user_link('lj','filin'),"tab instead of space"); + +=head2 title + +=cut + +is(MicroBlog::Format::find_user_links(""), + MicroBlog::Format::user_link('lj','filin','Artem'),"title"); + +is(MicroBlog::Format::find_user_links(""), + MicroBlog::Format::user_link('lj','filin','Other guy'),"Title with space"); + +is(MicroBlog::Format::find_user_links(""), + MicroBlog::Format::user_link('lj','filin','Other guy'),"Multiline"); + + +=head2 Multiple user names + +=cut + + +is(MicroBlog::Format::find_user_links(' and '), + 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("\n\n"), +MicroBlog::Format::user_link('lj','filin')."\n\n". + MicroBlog::Format::user_link('lj','kilka') + ,"Two user links on separate lines");