Skip to content

Instantly share code, notes, and snippets.

@kinichiro
Last active November 3, 2015 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinichiro/06b96692f186039b042e to your computer and use it in GitHub Desktop.
Save kinichiro/06b96692f186039b042e to your computer and use it in GitHub Desktop.
fix_fuzzing_by_changing_image_macro
This script fixes fuzzing caused by change of image: -> image::.
Search "#. type: Target for macro image" or
"#. type: Named 'alt' AttributeList argument for macro 'image'"
from po file, and set proper string to msgstr.
Usage:
image_macro.pl <lang>
To run:
1. place image_macro.pl under the kicad-doc/src/
2. cd kicad-doc/src/pcbnew/
3. touch *.adoc
4. make -f ../../utils/old-build-scripts/Makefile po/ja.po
5. ../image_macro.pl ja
6. po/ja.po.new is generated
#!/usr/bin/perl
use strict;
use utf8;
use open IN => ":utf8";
use open OUT => ":utf8";
use open IO => ":utf8";
use File::Basename qw/basename dirname/;
my($lang, $pofile, $newpo, $flag);
my($line, $buf);
my($type, $comment, $fuzzy, $last, $msgid, $msgstr, $msgstr_png);
my($msgid_png_base, $msgstr_png_base, $i18n_msgid_png);
$lang = $ARGV[0];
if( $lang eq "" ) {
print "usage: this-script-name <lang>\n";
exit(1);
}
print ":-| lang = $lang\n";
# open <lang>.po
$pofile = "po/$lang.po";
open(POFILE, "< $pofile") || die("can't open $pofile. $!");
# open new po file
$newpo = "$pofile.new";
open(NEWPO, "> $newpo") || die("can't open $newpo. $!");
#
# read po/<lang>.po and find "#. type: Target for macro image"
# then get $msgid and $msgstr.
#
$flag = 0;
while( <POFILE> ) {
chop;
$line = $_;
if($line eq "#. type: Target for macro image") {
$flag = 1;
$buf = $line . "\n";
$type = $line;
$comment = "";
$last = "";
} elsif($line eq "#. type: Named 'alt' AttributeList argument for macro 'image'") {
$flag = 2;
$buf = $line . "\n";
$type = $line;
$comment = "";
$last = "";
} elsif($flag != 0 && $line =~ /^#: /) {
$buf = $buf . $line . "\n";
$comment = $comment . $line ."\n";
} elsif($flag != 0 && $line =~ /^#\, /) {
$buf = $buf . $line . "\n";
$fuzzy = $line;
} elsif($flag != 0 && $line =~ /^#\| /) {
$buf = $buf . $line . "\n";
$last = $last . $line ."\n";
} elsif($flag != 0 && $line =~ /^msgid /) {
# get msgid
$buf = $buf . $line . "\n";
$line =~ s/(^msgid \")|(\"$)//g;
$msgid = $line;
while( <POFILE> ) {
chop;
$line = $_;
if($line =~ /^msgstr /) {
last;
}
$buf = $buf . $line . "\n";
$line =~ s/(^\")|(\"$)//g;
$msgid = $msgid . $line;
}
# get msgstr
$buf = $buf . $line . "\n";
$line =~ s/(^msgstr \")|(\"$)//g;
$msgstr = $line;
while( <POFILE> ) {
chop;
$line = $_;
if($line eq "") {
last;
}
$buf = $buf . $line . "\n";
$line =~ s/(^\")|(\"$)//g;
$msgstr = $msgstr . $line;
}
if($flag == 1) {
$msgstr_png = $msgstr;
$msgstr_png =~ s/(^image:*)|(\[.*\]$)//g;
$msgid_png_base = basename($msgid);
$msgstr_png_base = basename($msgstr_png);
$i18n_msgid_png = "images/$lang/$msgid_png_base";
if( -f $msgstr_png && $msgid_png_base eq $msgstr_png_base ) {
print ":-| set [$msgstr_png] and remove fuzzy.\n";
print NEWPO "$type\n";
print NEWPO "$comment";
print NEWPO "msgid \"$msgid\"\n";
print NEWPO "msgstr \"$msgstr_png\"\n";
print NEWPO "\n";
} elsif( -f $i18n_msgid_png ) {
print ":-| found [$i18n_msgid_png] and remove fuzzy.\n";
print NEWPO "$type\n";
print NEWPO "$comment";
print NEWPO "msgid \"$msgid\"\n";
print NEWPO "msgstr \"$i18n_msgid_png\"\n";
print NEWPO "\n";
} else {
print ":-| leave [$msgid] as fuzzy or untranslated.\n";
print NEWPO "$buf";
print NEWPO "\n";
}
} elsif ($flag == 2) {
print NEWPO "$type\n";
print NEWPO "$comment";
print NEWPO "msgid \"$msgid\"\n";
if($msgstr eq "") {
print NEWPO "msgstr \"$msgid\"\n";
} else {
print NEWPO "msgstr \"$msgstr\"\n";
}
print NEWPO "\n";
}
$flag = 0;
} else {
print NEWPO "$line\n";
}
}
# close new po file
close(NEWPO);
# close <lang>.po
close(POFILE);
print ":-| $newpo is created.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment