| 1 |
#!/usr/bin/perl |
| 2 |
# SPDX-FileCopyrightText: Copyright (c) 2021-2025 Yegor Bugayenko |
| 3 |
# SPDX-License-Identifier: MIT |
| 4 |
|
| 5 |
# 0.23.0 2025/12/24 |
| 6 |
package eolang; |
| 7 |
|
| 8 |
use warnings; |
| 9 |
use strict; |
| 10 |
use File::Basename; |
| 11 |
|
| 12 |
# Hash of incoming command line arguments. |
| 13 |
my %args = map { $_ => 1 } @ARGV; |
| 14 |
|
| 15 |
# Read file content. |
| 16 |
sub readfile { |
| 17 |
my ($path) = @_; |
| 18 |
open(my $h, '<', $path) or die('Cannot read from file: ' . $path); |
| 19 |
my $content; { local $/; $content = <$h>; } |
| 20 |
return $content; |
| 21 |
} |
| 22 |
|
| 23 |
# Save content to file. |
| 24 |
sub savefile { |
| 25 |
my ($path, $content) = @_; |
| 26 |
open(my $f, '>', $path) or error('Cannot open file for writing: ' . $path); |
| 27 |
print $f $content; |
| 28 |
close($f); |
| 29 |
} |
| 30 |
|
| 31 |
# Print INFO message to the console. |
| 32 |
sub info { |
| 33 |
my ($txt) = @_; |
| 34 |
print $txt . "\n"; |
| 35 |
} |
| 36 |
|
| 37 |
# Print DEBUG message to the console. |
| 38 |
sub debug { |
| 39 |
my ($txt) = @_; |
| 40 |
if (exists $args{'--verbose'}) { |
| 41 |
print $txt . "\n"; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
# Print ERROR message to the console. |
| 46 |
sub error { |
| 47 |
my ($txt) = @_; |
| 48 |
print STDERR $txt . "\n"; |
| 49 |
} |
| 50 |
|
| 51 |
if (@ARGV+0 eq 0 or exists $args{'--help'} or exists $args{'-?'}) { |
| 52 |
info("This script helps embedding \\phiquation and \\phiq into .tex document\n\n" . |
| 53 |
"Usage:\n" . |
| 54 |
" eolang [<options>] <.tex input file path> <.tex output file path>\n\n" . |
| 55 |
"Options:\n" . |
| 56 |
" -v, --version Print the current version of the tool and exit\n" . |
| 57 |
" -?, --help Print this help screen\n" . |
| 58 |
" --verbose Print all possible debugging information\n" . |
| 59 |
" --tmpdir=path Temp directory with .tex files ('_eolang' by default)\n\n" . |
| 60 |
"If any issues, report to GitHub: https://github.com/yegor256/bibcop"); |
| 61 |
} elsif (exists $args{'--version'} or exists $args{'-v'}) { |
| 62 |
info('0.23.0 2025/12/24'); |
| 63 |
} else { |
| 64 |
my ($src, $target) = grep { not($_ =~ /^-.*$/) } @ARGV; |
| 65 |
if (not $src) { |
| 66 |
error('Source file name must be specified'); |
| 67 |
exit(1); |
| 68 |
} |
| 69 |
debug('Source: ' . $src); |
| 70 |
my $job = basename($src); |
| 71 |
$job =~ s/\.[^.]+$//; |
| 72 |
debug('Job name: ' . $job); |
| 73 |
my $tex = readfile($src); |
| 74 |
my $tmpdir = dirname($src) . '/_eolang/' . $job; |
| 75 |
debug('EO tmpdir: ' . $tmpdir); |
| 76 |
foreach my $f (glob($tmpdir . '/*-phiq.tex')) { |
| 77 |
my $id = basename($f); |
| 78 |
$id =~ s/\.[^.]+$//; |
| 79 |
$id =~ s/-phiq$//; |
| 80 |
my $phiq = readfile($f); |
| 81 |
$phiq =~ s/^\s+|\s+$//g; |
| 82 |
my $search = quotemeta($phiq); |
| 83 |
$search =~ s/(\\\\[a-zA-Z]+)\\ /$1\\ ?/g; |
| 84 |
$search = '\\\\phiq\\s*\\{\\s*' . $search . '\\s*\\}|\\$\\s*' . $search . '\\s*\\$'; |
| 85 |
my $re = '\input{' . $tmpdir . '/' . $id . '-phiq-post.tex' . "}"; |
| 86 |
my $count = 0; |
| 87 |
while (1) { |
| 88 |
my $applied = $tex =~ s/${search}/${re}/g; |
| 89 |
if (!$applied) { |
| 90 |
if ($count eq 0) { |
| 91 |
debug("Neither \\phiq{$phiq} nor \$$phiq\$ found, suggested by $f"); |
| 92 |
} |
| 93 |
last; |
| 94 |
} |
| 95 |
debug('\\phiq ' . $id . '( ' . $phiq . ' ) -> ' . $re); |
| 96 |
$count += 1; |
| 97 |
} |
| 98 |
} |
| 99 |
my @kinds = ('sodg', 'phiquation', 'phiquation*'); |
| 100 |
for my $kind (@kinds) { |
| 101 |
my $k = $kind; |
| 102 |
$k =~ s/\*$//; |
| 103 |
foreach my $f (glob($tmpdir . '/*-' . $k . '.tex')) { |
| 104 |
my $id = basename($f); |
| 105 |
$id =~ s/\.[^.]+$//; |
| 106 |
$id =~ s/-${k}$//; |
| 107 |
my $search = quotemeta(readfile($f)); |
| 108 |
$search = '\\\\begin\\s*\\{\\s*' . quotemeta($kind) . '\\s*\\}\\n' . $search . '\\\\end\\s*\\{\\s*' . quotemeta($kind) . '\\s*\\}\\n'; |
| 109 |
my $re = '\input{' . $tmpdir . '/' . $id . '-' . $k . '-post.tex' . "}\% '$kind' replaced\n\n"; |
| 110 |
my $count = 0; |
| 111 |
while (1) { |
| 112 |
my $applied = $tex =~ s/${search}/${re}/g; |
| 113 |
if (!$applied) { |
| 114 |
if ($count eq 0) { |
| 115 |
debug("Didn't find \\begin{$kind} suggested by $f"); |
| 116 |
} |
| 117 |
last; |
| 118 |
} |
| 119 |
debug('\\begin{' . $kind . '} ' . $id . ' -> ' . $re); |
| 120 |
$count += 1; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
if (not $target) { |
| 125 |
error('Target file name must be specified'); |
| 126 |
exit(1); |
| 127 |
} |
| 128 |
debug('Target: ' . $target); |
| 129 |
savefile($target, $tex); |
| 130 |
} |
| 131 |
|
| 132 |
# In order to finish it with success: |
| 133 |
1; |