Permalink
Browse files

export to latex

  • Loading branch information...
1 parent 94acf28 commit a934ea099298bf34f20b7a0505a3d426deed1072 @zag committed with zag Dec 11, 2012
Showing with 204 additions and 44 deletions.
  1. +2 −1 Build.PL
  2. +109 −30 lib/WriteAt/To/Atom.pm
  3. +1 −3 lib/WriteAt/To/Latex.pm
  4. +32 −4 scripts/writeat
  5. +31 −6 t/01.bookinfo.t
  6. +29 −0 t/exportatom.pod6
View
@@ -6,7 +6,8 @@ Module::Build->new
prereq => {
'Regexp::Grammars'=>1.015,
'Perl6::Pod::Lib'=>0.11,
- 'Perl6::Pod'=>0.55
+ 'Perl6::Pod'=>0.55,
+ 'DateTime::Format::W3CDTF'=>0
},
script_files=>[qw!
scripts/writeat
View
@@ -7,15 +7,20 @@
=head1 NAME
-WriteAt - suite for make books and docs in pod6 format
+WriteAt::To::Atom - Atom renderer
=head1 SYNOPSIS
new WriteAt::To::Atom::
lang=>'en',
baseurl=>'http://example.com',
- #filepath for store htm files to
- basepath => ./
+ #set date to filter items by 'published' attr
+ set_date=> '2012-07-20T09:00:00Z'
+ | '2012-07-20 09:00:00'
+ | '2012-07-20',
+ #set all entries without 'published' attr
+ # to published anyway
+ default_published => 1
=cut
@@ -24,8 +29,9 @@ use strict;
use warnings;
use Perl6::Pod::Utl;
use WriteAt::To;
+use DateTime::Format::W3CDTF;
-#use Perl6::Pod::To::XHTML;
+use Perl6::Pod::To::XHTML;
use base ( 'Perl6::Pod::To::XHTML', 'WriteAt::To' );
use utf8;
@@ -36,22 +42,86 @@ sub new {
return $self;
}
+sub get_published {
+ my $self = shift;
+ my $node = shift || return;
+ return $node->get_attr->{published}
+
+ # unless
+}
+
+=head2 get_time_stamp_from_string <str>
+
+Get time stamp from strnigs like this:
+
+ 2012-11-27T09:39:19Z
+ 2012-11-27 09:39:19
+ 2012-11-27 09:39
+ 2012-11-27 09
+ 2012-11-27
+
+return unixtimestamp
+
+=cut
+
+sub get_time_stamp_from_string {
+ my $self = shift;
+ my $str = shift;
+
+ #if w3cdtf time
+ if ( $str =~ /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(Z|.\d{2}:\d{2})/ ) {
+ my $dt = DateTime::Format::W3CDTF->new();
+ return $dt->parse_datetime($str)->epoch();
+ }
+ elsif ( $str =~
+ /^(\d{4})-(\d{2})-(\d{2})(?:.(\d{2})(?::(\d{2})(?::(\d{2}))?)?)?/ )
+ {
+ my $dt = DateTime->new(
+ year => $1,
+ month => $2,
+ day => $3,
+ hour => $4 || 0,
+ minute => $5 || 0,
+ second => $6 || 0,
+ nanosecond => 500000000,
+ );
+ return $dt->epoch;
+ }
+ die "Bad srting $str";
+}
+
+=head2 unixtime_to_string timestamp
+
+Return
+
+=cut
+
+sub unixtime_to_string {
+ my $self = shift;
+ my $time = shift || return;
+ my ( $sec, $min, $hour, $mday, $mon, $year, $wday ) = gmtime($time);
+ $year += 1900;
+ return sprintf( "%04d-%02d-%02dT%02d:%02d:%02dZ",
+ $year, $mon + 1, $mday, $hour, $min, $sec );
+}
+
sub start_write {
- my $self = shift;
- my %tags = @_;
- my $w = $self->writer;
- my $title = &WriteAt::get_text( @{ $tags{TITLE} } );
- my $base_url = $self->{baseurl};
- my $author = &WriteAt::get_text( @{ $tags{AUTHOR} } );
- my $atomid = "tag:$base_url, 2012:1";
+ my $self = shift;
+ my %tags = @_;
+ my $w = $self->writer;
+ my $title = &WriteAt::get_text( @{ $tags{TITLE} } );
+ my $base_url = $self->{baseurl};
+ my $author = &WriteAt::get_text( @{ $tags{AUTHOR} } );
+ my $atomid = "tag:$base_url, 2012:1";
+ my $updated_time = $self->unixtime_to_string( time() );
$w->raw(<<TXT);
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>$title</title>
<id>$atomid</id>
<link rel="alternate" href="$base_url" type="text/html"/>
<link rel="self" href="$base_url/Atom.xml" type="application/atom+xml"/>
- <updated>2012-11-27T09:39:19Z</updated>
+ <updated>$updated_time</updated>
<author>
<name>$author</name>
<uri>$base_url</uri>
@@ -65,10 +135,32 @@ sub write {
my $res = &WriteAt::make_levels( "CHAPTER", 0, $tree );
my $w = $self->writer;
foreach my $entry (@$res) {
+
+ # check if need filter by published
+ my $published = $entry->{node}->get_attr->{published};
+ my $current_time = $self->{set_date} ?
+ $self->get_time_stamp_from_string( $self->{set_date} ) : time();
+ #if strict check published attr
+ unless ( $self->{default_published} ) {
+ #skip enties without published attr
+ next unless $published;
+ }
+
+ #set default publushed = current time
+ my $published_time =
+ $published
+ ? $self->get_time_stamp_from_string($published)
+ : $current_time;
+
+ #skip entry in future
+ next if $published_time > $current_time;
+
+ my $updated = $self->unixtime_to_string( time() );
+ $published ||= $updated;
my $title = &WriteAt::get_text( $entry->{node} );
my $title_en = &WriteAt::rus2lat($title);
- #clear spasec
+ #clear spaces
$title_en =~ s/\s+/-/g;
my $filename = $title_en . ".htm";
my $url = $self->{baseurl} . "/" . $filename;
@@ -78,8 +170,8 @@ sub write {
<title>$title</title>
<id>$id</id>
<link rel="alternate" href="$url" title="$title" type="text/html"/>
- <published>2012-07-20T09:00:00Z</published>
- <updated>2012-07-20T09:00:00Z</updated>
+ <published>$published</published>
+ <updated>$updated</updated>
<content type="html">%
);
my $out = '';
@@ -95,37 +187,24 @@ sub write {
$self->context->use->{$BLOCK_NAME};
}
- # $renderer->parse( \$text, default_pod=>1 );
- foreach my $n ( $entry->{node}, $entry->{childs} ) {
+ foreach my $n ( $entry->{childs} ) {
$renderer->visit($n);
}
+ close $fd;
my $safe_text = &Perl6::Pod::Writer::_html_escape($out);
$w->raw($safe_text);
$w->raw(
'</content>
</entry>'
);
- #if basepath
- if ( my $basepath = $self->{basepath} ) {
-
- # save content to htm file
- warn "$filename : File already exists" if -e $filename;
- my $content =
- $self->page_template( content => $out, title => $title );
-
- open FH, ">$filename" or die "$filename : $!";
- print FH $content;
- close FH;
- }
}
}
sub end_write {
my $self = shift;
$self->SUPER::end_write();
- $self->w->raw('</chapter>') if $self->{IN_CHAPTER};
$self->w->raw('</feed>');
}
@@ -21,7 +21,7 @@ sub start_write {
\usepackage[utf8]{inputenc}%включаем свою кодировку: koi8-r или utf8 в UNIX, cp1251 в Windows
\usepackage[english,russian]{babel}%используем русский и английский языки с переносами
\usepackage{amssymb,amsfonts,amsmath,mathtext,cite,enumerate,float} %подключаем нужные пакеты расширений
-\usepackage[dvips]{graphicx} %хотим вставлять в диплом рисунки?
+\usepackage[dvips]{graphicx} %хотим вставлять рисунки?
\graphicspath{{images/}}%путь к рисункам
\newcommand{\tocsecindent}{\hspace{7mm}}
\usepackage{makeidx}
@@ -82,8 +82,6 @@ sub block_CHAPTER {
sub block_para {
my ( $self, $el ) = @_;
-
- # $self->w->raw()
$self->visit( Perl6::Pod::Utl::parse_para( $el->childs->[0] ) );
}
View
@@ -10,6 +10,7 @@ use WriteAt;
use WriteAt::To::Latex;
use Perl6::Pod::Utl;
use Perl6::Pod::Lib;
+use WriteAt::To::Atom;
use constant {
TYPE2CLASS => {
docbook => 'WriteAt::To::DocBook',
@@ -81,8 +82,15 @@ unless ($type) {
push @{ $ini{$tag} }, @opt;
}
-
- my $r = $class->new( lang => $lang, %ini );
+ #clear -baseurl -> baseurl
+ # baseurl => ['http://example.com'] -> baseurl=>'http://example.com'
+ my %opt = ();
+ while ( my ($k, $v ) = each %ini ) {
+ $k =~ s/^-+//;
+ $opt{$k} = scalar(@$v) > 1 ? $v : $v->[0] ;
+
+ }
+ my $r = $class->new( lang => $lang, %opt );
#set src key for path
$r->context->custom->{src} = $infile;
@@ -146,15 +154,35 @@ exit 0;
=head1 SYNOPSIS
writeat -type docbook file.pod6 > file.xml
+
+or
+ writeat [options] [file] -- [render oprions]
+
- options:
+ [options]:
-help - print help message
-man - print man page
-type - output format (docbook, latex, atom)
-render,-r - use class for render (option -type ignored)
-lang - set language ( default: en )
-
+
+ [file] : filename or - for STDIN
+
+ [render options]:
+ atom:
+ -baseurl http://example.org - base url for links
+ -default_publish 1 - publish items without 'published' attr
+ default: 0.
+ -set_date '2012-11-27T09:39:18Z' - set time to filter published
+
+ atom example:
+
+ writeat -t atom -lang en - \
+ -- -baseurl http://example.org \
+ -default_publish 0 \
+ -set_date '2012-11-27T09:39:18Z' < exportatom.pod6
+
=head1 OPTIONS
View
@@ -32,7 +32,8 @@ Jan 08th 2011(v0.5)[zag] Подпрограммы и сигнатуры
=end CHANGES
=AUTHOR Александр Загацкий
-=CHAPTER Test chapter
+=for CHAPTER :published<'2012-11-27T09:39:19Z'>
+Test chapter
Ok
=head1 Test name
@@ -61,21 +62,45 @@ $tree = &WriteAt::get_book_info_blocks( $tree, \%res );
#print Dumper $tree; exit;
my $res = &WriteAt::make_levels( "CHAPTER", 0, $tree );
-is scalar(@$res), 2 , 'Get semantic nodes';
-is &WriteAt::get_text($res->[0]->{node}), 'Test chapter', 'get text content of node';
+is scalar(@$res), 2, 'Get semantic nodes';
+is &WriteAt::get_text( $res->[0]->{node} ), 'Test chapter',
+ 'get text content of node';
my %res2 = ();
my $tree2 = Perl6::Pod::Utl::parse_pod( $t, default_pod => 1 )
|| die "Can't parse ";
$tree2 = &WriteAt::get_book_info_blocks( $tree2, \%res2 );
-#diag Dumper $res->[0];
+
use_ok "WriteAt::To::Atom";
-my $atom = new WriteAt::To::Atom:: lang => 'en';
-diag $atom->writer;
+my $out = '';
+open( my $fd, ">", \$out );
+
+my $atom = new WriteAt::To::Atom::
+ lang => 'en',
+ default_published => 0,
+ set_date => '2012-12-15T13:00:00Z',
+ writer => new Perl6::Pod::Writer( out => $fd, escape => 'xml' );
+
+is my $utc = $atom->get_time_stamp_from_string('2003-02-15T13:00:00Z'),
+ $atom->get_time_stamp_from_string('2003-02-15T12:00:00-01:00'),
+ "Get timestams";
+is $utc, $atom->get_time_stamp_from_string('2003-02-15 13:00'),
+ "2003-02-15 13:00";
+is $utc, $atom->get_time_stamp_from_string('2003-02-15 13'), "2003-02-15 13";
+is $atom->get_time_stamp_from_string('2003-02-15T00:00:00Z'),
+ $atom->get_time_stamp_from_string('2003-02-15'),
+ "2003-02-15";
+
+is $atom->unixtime_to_string(
+ $atom->get_time_stamp_from_string('2003-01-15T06:00:00-02:00') ),
+ '2003-01-15T08:00:00Z', 'unixtime_to_string';
+
$atom->start_write(%res2);
$atom->write($tree2);
$atom->end_write();
+close $fd;
+is scalar( @{ [ $out =~ /(<\/entry>)/gs ] } ), 1, 'default_published';
#$atom
#print Dumper \%res; exit;
View
@@ -0,0 +1,29 @@
+=begin pod
+=TITLE Main title
+=SUBTITLE mini subtitle
+=DESCRIPTION
+asd asd
+=begin CHANGES
+Dec 09th 2012(v0.1)[zag] Start
+
+=end CHANGES
+=AUTHOR Alex Zahatski
+=for CHAPTER :published<'2012-11-27T09:39:19Z'>
+Test chapter
+
+Ok
+=head1 Test name
+=head2 test
+
+yes
+
+=head1 Test 1
+
+ok
+
+=CHAPTER Test chapter2
+
+Ok
+=head1 Teste
+
+=end pod

0 comments on commit a934ea0

Please sign in to comment.