summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/latexindent/LatexIndent/TrailingComments.pm
blob: f8987d271f1ee24ddfb66c8fb5fba89b42cd7ff4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package LatexIndent::TrailingComments;
#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	See http://www.gnu.org/licenses/.
#
#	Chris Hughes, 2017
#
#	For all communication, please visit: https://github.com/cmhughes/latexindent.pl
use strict;
use warnings;
use LatexIndent::Tokens qw/%tokens/;
use LatexIndent::Switches qw/$is_t_switch_active $is_tt_switch_active/;
use Data::Dumper;
use Exporter qw/import/;
our @EXPORT_OK = qw/remove_trailing_comments put_trailing_comments_back_in $trailingCommentRegExp add_comment_symbol construct_trailing_comment_regexp/;
our @trailingComments;
our $commentCounter = 0;
our $trailingCommentRegExp;

sub construct_trailing_comment_regexp{
    $trailingCommentRegExp = qr/(?<!\\)%$tokens{trailingComment}\d+$tokens{endOfToken}/;
}

sub add_comment_symbol{
    # add a trailing comment token after, for example, a square brace [
    # or a curly brace { when, for example, BeginStartsOnOwnLine == 2
    my $self = shift;

    # increment the comment counter
    $commentCounter++;

    # store the comment -- without this, it won't get processed correctly at the end
    push(@trailingComments,{id=>$tokens{trailingComment}.$commentCounter.$tokens{endOfToken},value=>q()});

    # log file info
    $self->logger("Updating trailing comment array",'heading')if $is_t_switch_active;
    $self->logger(Dumper(\@trailingComments),'ttrace') if($is_tt_switch_active);

    # the returned value
    return $tokens{trailingComment}.$commentCounter.$tokens{endOfToken};
}

sub remove_trailing_comments{
    my $self = shift;
    $self->logger("Storing trailing comments",'heading')if $is_t_switch_active;

    # perform the substitution
    ${$self}{body} =~ s/
                            (?<!\\)  # not preceeded by a \
                            %        # % 
                            (
                                \h*? # followed by possible horizontal space
                                .*?  # and anything else
                            )
                            $        # up to the end of a line
                        /   
                            # increment comment counter and store comment
                            $commentCounter++;
                            push(@trailingComments,{id=>$tokens{trailingComment}.$commentCounter.$tokens{endOfToken},value=>$1});

                            # replace comment with dummy text
                            "%".$tokens{trailingComment}.$commentCounter.$tokens{endOfToken};
                       /xsmeg;
    if(@trailingComments){
        $self->logger("Trailing comments stored in:") if($is_t_switch_active);
        $self->logger(Dumper(\@trailingComments)) if($is_t_switch_active);
    } else {
        $self->logger("No trailing comments found") if($is_t_switch_active);
    }
    return;
}

sub put_trailing_comments_back_in{
    my $self = shift;
    return unless( @trailingComments > 0 );

    $self->logger("Returning trailing comments to body",'heading')if $is_t_switch_active;

    # loop through trailing comments in reverse so that, for example, 
    # latexindenttrailingcomment1 doesn't match the first 
    # part of latexindenttrailingcomment18, which would result in an 8 left over (bad)
    while( my $comment = pop @trailingComments){
      my $trailingcommentID = ${$comment}{id};
      my $trailingcommentValue = ${$comment}{value};
      if(${$self}{body} =~ m/%$trailingcommentID
                              (
                                  (?!          # not immediately preceeded by 
                                      (?<!\\)  # \
                                      %        # %
                                  ).*?
                              )                # captured into $1
                              (\h*)?$                
                          /mx and $1 ne ''){
          $self->logger("Comment not at end of line $trailingcommentID, moving it to end of line")if $is_t_switch_active;
          ${$self}{body} =~ s/%$trailingcommentID(.*)$/$1%$trailingcommentValue/m;
      } else {
          ${$self}{body} =~ s/%$trailingcommentID/%$trailingcommentValue/;
      }
      $self->logger("replace %$trailingcommentID with %$trailingcommentValue") if($is_t_switch_active);
    }
    return;
}

1;