| 1 | package Padre::Document::LaTeX::Syntax; | ||
| 2 | BEGIN { | ||
| 3 | $Padre::Document::LaTeX::Syntax::VERSION = '0.13'; | ||
| 4 | } | ||
| 5 | |||
| 6 | # ABSTRACT: Latex document syntax-checking in the background | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use warnings; | ||
| 10 | use Padre::Task::Syntax (); | ||
| 11 | |||
| 12 | our @ISA = 'Padre::Task::Syntax'; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | |||
| 17 | my %args = @_; | ||
| 18 | my $self = $class->SUPER::new(%args); | ||
| 19 | |||
| 20 | return $self; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub _call_pdflatex { | ||
| 24 | my ( $self, $text ) = @_; | ||
| 25 | |||
| 26 | my $project_dir = $self->{project}; | ||
| 27 | |||
| 28 | # create temporary directory and LaTeX file | ||
| 29 | require File::Temp; | ||
| 30 | my $tempdir = File::Temp::tempdir( 'Padre-Document-LaTeX-Syntax-XXXXXX', TMPDIR => 1 ); | ||
| 31 | my $file = File::Temp->new( | ||
| 32 | TEMPLATE => 'XXXXXX', | ||
| 33 | UNLINK => 1, DIR => $project_dir | ||
| 34 | ); | ||
| 35 | |||
| 36 | # write text to temporary file | ||
| 37 | my $filename = $file->filename; | ||
| 38 | binmode( $file, ':utf8' ); | ||
| 39 | $file->print($text); | ||
| 40 | $file->close; | ||
| 41 | |||
| 42 | # run pdflatex | ||
| 43 | my $pdflatex_command = | ||
| 44 | "cd $project_dir; pdflatex -file-line-error -draftmode -interaction nonstopmode -output-directory $tempdir $filename"; | ||
| 45 | |||
| 46 | #warn "$pdflatex_command\n"; | ||
| 47 | my $output = `$pdflatex_command`; | ||
| 48 | |||
| 49 | eval { | ||
| 50 | |||
| 51 | # clean up | ||
| 52 | require File::Path; | ||
| 53 | File::Path::remove_tree($tempdir); | ||
| 54 | }; | ||
| 55 | warn "$@\n" if $@; | ||
| 56 | |||
| 57 | return $output; | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | sub syntax { | ||
| 62 | my $self = shift; | ||
| 63 | my $text = shift; | ||
| 64 | |||
| 65 | my $output = $self->_call_pdflatex($text); | ||
| 66 | |||
| 67 | #warn "OUTPUT: $output\n"; | ||
| 68 | |||
| 69 | my @lines = split /\n/, $output; | ||
| 70 | my @issues = (); | ||
| 71 | |||
| 72 | LINE: | ||
| 73 | for ( my $i = 0; $i < scalar @lines; $i++ ) { | ||
| 74 | my $line = $lines[$i]; | ||
| 75 | |||
| 76 | next LINE if not $line =~ /.*:(\d+):\s*(.*)/; | ||
| 77 | my $line_no = $1; | ||
| 78 | my $error_msg = $2; | ||
| 79 | |||
| 80 | while ( ++$i < scalar @lines && $lines[$i] !~ /^\s*$/ && $lines[$i] !~ /<recently read>/ ) { | ||
| 81 | $lines[$i] =~ s/^l\.\d+ / /; | ||
| 82 | $error_msg .= $lines[$i]; | ||
| 83 | } | ||
| 84 | |||
| 85 | $error_msg =~ s/\s+/ /g; | ||
| 86 | |||
| 87 | #warn "error msg '$error_msg'\n"; | ||
| 88 | |||
| 89 | my %issue = ( | ||
| 90 | line => $line_no, | ||
| 91 | file => $self->{filename}, | ||
| 92 | type => 'F', | ||
| 93 | message => $error_msg, | ||
| 94 | ); | ||
| 95 | |||
| 96 | push @issues, \%issue; | ||
| 97 | } | ||
| 98 | my $num_issues = scalar @issues; | ||
| 99 | |||
| 100 | #warn "pdflatex output parsing: found $num_issues issues.\n"; | ||
| 101 | |||
| 102 | return \@issues; | ||
| 103 | } | ||
| 104 | |||
| 105 | 1; | ||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | =pod | ||
| 110 | |||
| 111 | =head1 NAME | ||
| 112 | |||
| 113 | Padre::Document::LaTeX::Syntax - Latex document syntax-checking in the background | ||
| 114 | |||
| 115 | =head1 VERSION | ||
| 116 | |||
| 117 | version 0.13 | ||
| 118 | |||
| 119 | =head1 SYNOPSIS | ||
| 120 | |||
| 121 | Syntax checking for LaTeX documents | ||
| 122 | |||
| 123 | =head1 DESCRIPTION | ||
| 124 | |||
| 125 | This class implements syntax checking of LaTeX documents in | ||
| 126 | the background. It inherits from L<Padre::Task::Syntax>. | ||
| 127 | Please read its documentation! | ||
| 128 | |||
| 129 | =head1 SEE ALSO | ||
| 130 | |||
| 131 | This class inherits from L<Padre::Task::Syntax> which | ||
| 132 | in turn is a L<Padre::Task> and its instances can be scheduled | ||
| 133 | using L<Padre::TaskManager>. | ||
| 134 | |||
| 135 | =head1 AUTHORS | ||
| 136 | |||
| 137 | =over 4 | ||
| 138 | |||
| 139 | =item * | ||
| 140 | |||
| 141 | Zeno Gantner <zenog@cpan.org> | ||
| 142 | |||
| 143 | =item * | ||
| 144 | |||
| 145 | Ahmad M. Zawawi <ahmad.zawawi@gmail.com> | ||
| 146 | |||
| 147 | =back | ||
| 148 | |||
| 149 | =head1 COPYRIGHT AND LICENSE | ||
| 150 | |||
| 151 | This software is copyright (c) 2011 by Zeno Gantner, Ahmad M. Zawawi. | ||
| 152 | |||
| 153 | This is free software; you can redistribute it and/or modify it under | ||
| 154 | the same terms as the Perl 5 programming language system itself. | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | |||
| 159 | __END__ |