Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add latex support

  • Loading branch information...
commit 6e88a5b096e25b2df9440711322b15afa83b15ac 1 parent 9576987
@zag authored
Showing with 208 additions and 30 deletions.
  1. +38 −9 lib/WriteAt/To/DocBook.pm
  2. +148 −0 lib/WriteAt/To/Latex.pm
  3. +22 −21 scripts/writeat
View
47 lib/WriteAt/To/DocBook.pm
@@ -5,40 +5,68 @@
# AUTHOR: Aliaksandr P. Zahatski, <zahatski@gmail.com>
#===============================================================================
package WriteAt::To::DocBook;
+use strict;
+use warnings;
use Perl6::Pod::Utl;
use WriteAt::To;
-use base ('Perl6::Pod::To::DocBook', 'WriteAt::To');
+use base ( 'Perl6::Pod::To::DocBook', 'WriteAt::To' );
use utf8;
+sub start_write {
+ my $self = shift;
+ my $w = $self->writer;
+ my $dtd = '';
+ for (
+ '/usr/local/share/xml/docbook/4.5/docbookx.dtd',
+ '/usr/share/xml/docbook/schema/dtd/4.5/docbookx.dtd'
+ )
+ {
+ if ( -e $_ ) {
+ $dtd = $_;
+ last;
+ }
+ }
+ die "Can't find docbookx.dtd file" unless $dtd;
+ my $lang = $self->{lang} || 'en';
+ $w->raw(<<"H");
+<?xml version='1.0' encoding="UTF-8"?>
+<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3CR1//EN"
+ "file://${dtd}" []>
+<book lang="$lang">
+H
+}
+
sub block_DESCRIPTION {
- my ($self, $n) = @_;
+ my ( $self, $n ) = @_;
my $w = $self->w;
$w->raw('<abstract>');
$self->visit_childs($n);
$w->raw('</abstract>');
-};
+}
-sub block_TITLE{
- my ($self, $n) = @_;
+sub block_TITLE {
+ my ( $self, $n ) = @_;
my $w = $self->w;
$w->raw('<title>');
$self->visit_childs($n);
$w->raw('</title>');
-};
+}
-sub block_SUBTITLE{
- my ($self, $n) = @_;
+sub block_SUBTITLE {
+ my ( $self, $n ) = @_;
my $w = $self->w;
$w->raw('<subtitle>');
$self->visit_childs($n);
$w->raw('</subtitle>');
-};
+}
+no strict;
# alias for CHAPTER
sub block_ГЛАВА {
$self = shift;
return $self->block_CHAPTER(@_)
}
+use strict;
sub block_CHAPTER {
@@ -55,6 +83,7 @@ sub end_write {
my $self = shift;
$self->SUPER::end_write();
$self->w->raw('</chapter>') if $self->{IN_CHAPTER};
+ $self->w->raw('</book>');
}
1;
View
148 lib/WriteAt/To/Latex.pm
@@ -0,0 +1,148 @@
+#===============================================================================
+#
+# DESCRIPTION: export to latex
+#
+# AUTHOR: Aliaksandr P. Zahatski, <zahatski@gmail.com>
+#===============================================================================
+package WriteAt::To::Latex;
+use strict;
+use warnings;
+use Perl6::Pod::Utl;
+use WriteAt::To;
+use base ( 'Perl6::Pod::To::DocBook', 'WriteAt::To' );
+use utf8;
+
+sub start_write {
+ my $self = shift;
+ $self->w->raw(<<'START');
+\documentclass[a4paper,12pt,twoside]{report} %размер бумаги устанавливаем А4, шрифт 12пунктов
+\usepackage[T2A]{fontenc}
+\usepackage{multicol}
+\usepackage[utf8]{inputenc}%включаем свою кодировку: koi8-r или utf8 в UNIX, cp1251 в Windows
+\usepackage[english,russian]{babel}%используем русский и английский языки с переносами
+\usepackage{amssymb,amsfonts,amsmath,mathtext,cite,enumerate,float} %подключаем нужные пакеты расширений
+\usepackage[dvips]{graphicx} %хотим вставлять в диплом рисунки?
+\graphicspath{{images/}}%путь к рисункам
+\newcommand{\tocsecindent}{\hspace{7mm}}
+\usepackage{makeidx}
+\usepackage{index}
+\newindex{aut}{adx}{and}{Name Index}
+\makeindex
+\makeatletter
+\renewcommand{\@biblabel}[1]{#1.} % Заменяем библиографию с квадратных скобок на точку:
+\makeatother
+
+\usepackage{geometry} % Меняем поля страницы
+\geometry{left=2cm}% левое поле
+\geometry{right=1.5cm}% правое поле
+\geometry{top=1cm}% верхнее поле
+\geometry{bottom=2cm}% нижнее поле
+
+\renewcommand{\theenumi}{\arabic{enumi}}% Меняем везде перечисления на цифра.цифра
+\renewcommand{\labelenumi}{\arabic{enumi}}% Меняем везде перечисления на цифра.цифра
+\renewcommand{\theenumii}{.\arabic{enumii}}% Меняем везде перечисления на цифра.цифра
+\renewcommand{\labelenumii}{\arabic{enumi}.\arabic{enumii}.}% Меняем везде перечисления на цифра.цифра
+\renewcommand{\theenumiii}{.\arabic{enumiii}}% Меняем везде перечисления на цифра.цифра
+\renewcommand{\labelenumiii}{\arabic{enumi}.\arabic{enumii}.\arabic{enumiii}.}% Меняем везде перечисления на цифра.цифра
+
+\begin{document}
+START
+}
+
+sub end_write {
+ my $self = shift;
+ $self->w->raw(
+ '\clearpage
+\addcontentsline{toc}{chapter}{Index}
+\printindex
+'
+ );
+ $self->w->raw('\end{document}');
+}
+
+sub block_CHAPTER {
+ my ( $self, $node ) = @_;
+ my $attr = $node->get_attr;
+
+ #close any section
+ $self->switch_head_level(0);
+ my $title = $node->childs->[0]->childs->[0];
+ if ( $attr->{preface} ) {
+ $self->w->raw( '\section*{' . $title . '}' );
+ $self->w->raw(
+ '\addcontentsline{toc}{section}
+ {\tocsecindent{' . $title . '}}'
+ );
+
+ }
+ else {
+ $self->w->raw( '\section{' . $title . '}' );
+ }
+}
+
+sub block_para {
+ my ( $self, $el ) = @_;
+
+ # $self->w->raw()
+ $self->visit( Perl6::Pod::Utl::parse_para( $el->childs->[0] ) );
+
+}
+
+sub title_page {
+ my $self = shift;
+ my %SEMS = @_;
+ my %sems = ();
+
+ #get text nodes;
+ foreach my $k ( keys %SEMS ) {
+ my $v = $SEMS{$k};
+ foreach my $n (@$v) {
+ for ( @{ $n->childs } ) {
+ my $txt = $_->childs->[0];
+ chomp $txt;
+ push @{ $sems{$k} }, $txt;
+ }
+
+ }
+ }
+ $self->w->raw(<<'TEXT');
+\begin{titlepage}
+\newpage
+
+\begin{center}
+\end{center}
+\vspace{6em}
+\hrulefill
+
+\begin{center}
+TEXT
+$self->w->raw('\Large '.$sems{TITLE}->[0]);
+$self->w->raw(<<'TEXT');
+\end{center}
+\hrulefill
+
+\vspace{2.5em}
+
+\begin{center}
+TEXT
+ $self->w->raw('\textsc{\textbf{'.$sems{SUBTITLE}->[0].'}}');
+
+
+ $self->w->raw('\begin{center}\textsc{');
+foreach my $author (@{$sems{AUTHOR}}) {
+ $self->w->raw( $author. '\\\\');
+}
+ $self->w->raw('}\end{center}');
+$self->w->raw('\end{center}');
+
+$self->w->raw('
+\vspace{\fill}
+
+\begin{center}
+WriteAt
+\end{center}
+
+\end{titlepage}');
+}
+1;
+
View
43 scripts/writeat
@@ -7,6 +7,7 @@ use v5.10;
use Data::Dumper;
use IO::File;
use WriteAt;
+use WriteAt::To::Latex;
use Perl6::Pod::Utl;
use Perl6::Pod::Lib;
@@ -41,7 +42,9 @@ unless ($type) {
utf8::decode($in) unless utf8::is_utf8($in);
my $tree = Perl6::Pod::Utl::parse_pod( $in, default_pod => 1 )
|| die "Can't parse $infile";
- my $r = new WriteAt::To::DocBook::;
+ #create driver instance
+ my $r = $type eq 'docbook' ? new WriteAt::To::DocBook:: lang=>$lang
+ : new WriteAt::To::Latex:: lang=>$lang;
#set src key for path
$r->context->custom->{src} = $infile;
@@ -57,27 +60,12 @@ unless ($type) {
$r->context->use->{AUTHOR} = 'WriteAt::AUTHOR';
my $w = $r->writer();
- my $dtd = '';
- for (
- '/usr/local/share/xml/docbook/4.5/docbookx.dtd',
- '/usr/share/xml/docbook/schema/dtd/4.5/docbookx.dtd'
- )
- {
- if ( -e $_ ) {
- $dtd = $_;
- last;
- }
- }
- die "Can't find docbookx.dtd file" unless $dtd;
- $w->raw(<<"H");
-<?xml version='1.0' encoding="UTF-8"?>
-<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3CR1//EN"
- "file://${dtd}" []>
-<book lang="$lang">
-<bookinfo>
-H
+ $r->start_write();
my %res;
$tree = &WriteAt::get_book_info_blocks( $tree, \%res, $r );
+
+ if ( $type eq 'docbook' ) {
+ $r->w->raw('<bookinfo>');
for (qw/ TITLE SUBTITLE AUTHOR CHANGES DESCRIPTION /) {
my $n = $res{$_} || die "Cant find block =$_";
@@ -85,9 +73,22 @@ H
$r->visit($n);
}
$r->w->raw('</bookinfo>');
+ } elsif ($type eq 'latex') {
+
+
+ $r->title_page( %res );
+ $r->w->raw('\tableofcontents{} % это оглавление, которое генерируется автоматически
+\newpage');
+# for (qw/ TITLE SUBTITLE AUTHOR CHANGES DESCRIPTION /) {
+# my $n = $res{$_} || die "Cant find block =$_";
+#
+# #make Document element
+# $r->visit($n);
+# }
+ }
+
$r->write($tree);
$r->end_write();
- $w->raw('</book>');
}
Please sign in to comment.
Something went wrong with that request. Please try again.