-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheolang.pl
More file actions
executable file
·133 lines (123 loc) · 3.83 KB
/
eolang.pl
File metadata and controls
executable file
·133 lines (123 loc) · 3.83 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/perl
# SPDX-FileCopyrightText: Copyright (c) 2021-2026 Yegor Bugayenko
# SPDX-License-Identifier: MIT
# 0.0.0 0000-00-00
package eolang;
use warnings;
use strict;
use File::Basename;
# Hash of incoming command line arguments.
my %args = map { $_ => 1 } @ARGV;
# Read file content.
sub readfile {
my ($path) = @_;
open(my $h, '<', $path) or die('Cannot read from file: ' . $path);
my $content; { local $/; $content = <$h>; }
return $content;
}
# Save content to file.
sub savefile {
my ($path, $content) = @_;
open(my $f, '>', $path) or error('Cannot open file for writing: ' . $path);
print $f $content;
close($f);
}
# Print INFO message to the console.
sub info {
my ($txt) = @_;
print $txt . "\n";
}
# Print DEBUG message to the console.
sub debug {
my ($txt) = @_;
if (exists $args{'--verbose'}) {
print $txt . "\n";
}
}
# Print ERROR message to the console.
sub error {
my ($txt) = @_;
print STDERR $txt . "\n";
}
if (@ARGV+0 eq 0 or exists $args{'--help'} or exists $args{'-?'}) {
info("This script helps embedding \\phiquation and \\phiq into .tex document\n\n" .
"Usage:\n" .
" eolang [<options>] <.tex input file path> <.tex output file path>\n\n" .
"Options:\n" .
" -v, --version Print the current version of the tool and exit\n" .
" -?, --help Print this help screen\n" .
" --verbose Print all possible debugging information\n" .
" --tmpdir=path Temp directory with .tex files ('_eolang' by default)\n\n" .
"If any issues, report to GitHub: https://github.com/yegor256/bibcop");
} elsif (exists $args{'--version'} or exists $args{'-v'}) {
info('0.0.0 0000-00-00');
} else {
my ($src, $target) = grep { not($_ =~ /^-.*$/) } @ARGV;
if (not $src) {
error('Source file name must be specified');
exit(1);
}
debug('Source: ' . $src);
my $job = basename($src);
$job =~ s/\.[^.]+$//;
debug('Job name: ' . $job);
my $tex = readfile($src);
my $tmpdir = dirname($src) . '/_eolang/' . $job;
debug('EO tmpdir: ' . $tmpdir);
foreach my $f (glob($tmpdir . '/*-phiq.tex')) {
my $id = basename($f);
$id =~ s/\.[^.]+$//;
$id =~ s/-phiq$//;
my $phiq = readfile($f);
$phiq =~ s/^\s+|\s+$//g;
my $search = quotemeta($phiq);
$search =~ s/(\\\\[a-zA-Z]+)\\ /$1\\ ?/g;
$search = '\\\\phiq\\s*\\{\\s*' . $search . '\\s*\\}|\\$\\s*' . $search . '\\s*\\$';
my $re = '\input{' . $tmpdir . '/' . $id . '-phiq-post.tex' . "}";
my $count = 0;
while (1) {
my $applied = $tex =~ s/${search}/${re}/g;
if (!$applied) {
if ($count eq 0) {
debug("Neither \\phiq{$phiq} nor \$$phiq\$ found, suggested by $f");
}
last;
}
debug('\\phiq ' . $id . '( ' . $phiq . ' ) -> ' . $re);
$count += 1;
}
}
my @kinds = ('sodg', 'phiquation', 'phiquation*');
for my $kind (@kinds) {
my $k = $kind;
$k =~ s/\*$//;
foreach my $f (glob($tmpdir . '/*-' . $k . '.tex')) {
my $id = basename($f);
$id =~ s/\.[^.]+$//;
$id =~ s/-${k}$//;
my $search = quotemeta(readfile($f));
$search = '\\\\begin\\s*\\{\\s*' . quotemeta($kind) . '\\s*\\}\\n' . $search . '\\\\end\\s*\\{\\s*' . quotemeta($kind) . '\\s*\\}\\n';
my $re = '\input{' . $tmpdir . '/' . $id . '-' . $k . '-post.tex' . "}\% '$kind' replaced\n\n";
my $count = 0;
while (1) {
my $applied = $tex =~ s/${search}/${re}/g;
if (!$applied) {
if ($count eq 0) {
debug("Didn't find \\begin{$kind} suggested by $f");
}
last;
}
debug('\\begin{' . $kind . '} ' . $id . ' -> ' . $re);
$count += 1;
}
}
}
if (not $target) {
error('Target file name must be specified');
exit(1);
}
debug('Target: ' . $target);
savefile($target, $tex);
}
# In order to finish it with success:
1;