Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # File: synctex-inclpdf-alpha.sh
- # License: LPPL, version 1.3 or later
- # Version: 0.0.0.1
- # Date: 26-05-2014
- # Author: Birk Eisermann
- if [ $# -ne 3 ]; then
- echo ""
- echo "usage: $0 'main' 'incl_1' 'incl_2' ..."
- echo " 'main' filename of the main file without extension,"
- echo " e.g. 'combined' for 'combined.synctex.gz'"
- echo " 'incl_N' filename of the N-th included pdf without extension,"
- echo " e.g. 'subdir/./incl1' for 'subdir/./incl1.synctex.gz'"
- echo ""
- echo "Note: This is a first draft where certain things are hard coded."
- echo "Requirements:"
- echo " - one main file with 1 page followed by includepdf's."
- echo " - exactly *two* includepdf with each having exactly two pages."
- echo "You can change the content of a page but due to the re-boxing by"
- echo "the package pdfpages the inverse search can be off by several lines;"
- echo "Nevertheless the page number should be correct."
- exit 1
- fi
- ### Step 1
- SYNCGZ=.synctex.gz
- SYNC=.synctex
- for SFILE; do # unzipping all files + save main file
- gunzip -c $SFILE$SYNCGZ > $SFILE$SYNC
- done
- mv $1$SYNC $1.0$SYNC
- mv $1$SYNCGZ $1.0$SYNCGZ
- OUT="$1.1$SYNC"
- #returns: jmain, inputoffset, maxpages(not used yet)
- awk -v root=$1 '
- BEGIN { FS=":"; state=0; input=0; maxpages=0; jmain=-1;
- texfilename=root ".tex";
- }
- /Input/ { if (state == 0) {
- input+=1;
- print "Input:" input ":" $3;
- if (index($3, texfilename) > 0) { jmain=input; }
- next;
- }
- }
- /Output/ { state=1; }
- /\{/ { maxpages+=1; }
- { if (state==1){next}; print; }
- END { inputoffset=input;
- print "---- inputoffset=" inputoffset "; jmain=" jmain "; maxpages=" maxpages ";";
- }
- ' $1.0$SYNC > $OUT
- ### Step 2
- #returns arrays index by incl: inputfrom, inputto, pagefrom, pageto, texinput
- # by input: inputfiles
- awk -v inclfilename=$2 -v incl=1 -v inputoffset=100 -v pageoffset=1 '
- BEGIN { FS=":"; state=10; input=0; page=0;
- filename=FILENAME; texfilename=inclfilename ".tex"; texinput[incl]=-1;
- print "---- texfilename="texfilename;
- }
- /Input/ { if (state == 10) {
- input+=1;
- inputfiles[inputoffset+input]=$3;
- if (index($3, texfilename) > 0) {
- texinput[incl]=inputoffset+input;
- }
- print "Input:" inputoffset+input ":" $3;
- next;
- }
- }
- /Output/ { state=11; }
- /\{/ { page+=1; }
- { }
- END { inputfrom[incl]=inputoffset+1;
- inputto[incl]=inputoffset+input;
- inputoffset=inputto[incl];
- pagefrom[incl]=pageoffset+1;
- pageto[incl]=pageoffset+page;
- pageoffset=pageto[incl];
- print "---- inputfrom["incl"]=" inputfrom[incl] ", inputto["incl"]=" inputto[incl] ";";
- print "---- pagefrom["incl"]=" pagefrom[incl] ", pageto["incl"]=" pageto[incl] ";"
- print "---- texinput["incl"]=" texinput[incl] ";";
- print "---- inputfiles["inputto[incl]"]=" inputfiles[inputto[incl]] ";";
- print "---- inputoffset=" inputoffset ", pageoffset=" pageoffset";";
- }
- ' $2$SYNC >> $OUT
- awk -v inclfilename=$3 -v incl=2 -v inputoffset=200 -v pageoffset=3 '
- BEGIN { FS=":"; state=10; input=0; page=0;
- filename=FILENAME; texfilename=inclfilename ".tex"; texinput[incl]=-1;
- print "---- texfilename="texfilename;
- }
- /Input/ { if (state == 10) {
- input+=1;
- inputfiles[inputoffset+input]=$3;
- if (index($3, texfilename) > 0) {
- texinput[incl]=inputoffset+input;
- }
- print "Input:" inputoffset+input ":" $3;
- next;
- }
- }
- /Output/ { state=11; }
- /\{/ { page+=1; }
- { }
- END { inputfrom[incl]=inputoffset+1;
- inputto[incl]=inputoffset+input;
- inputoffset=inputto[incl];
- pagefrom[incl]=pageoffset+1;
- pageto[incl]=pageoffset+page;
- pageoffset=pageto[incl];
- print "---- inputfrom["incl"]=" inputfrom[incl] ", inputto["incl"]=" inputto[incl] ";";
- print "---- pagefrom["incl"]=" pagefrom[incl] ", pageto["incl"]=" pageto[incl] ";"
- print "---- texinput["incl"]=" texinput[incl] ";";
- print "---- inputfiles["inputto[incl]"]=" inputfiles[inputto[incl]] ";";
- print "---- inputoffset=" inputoffset ", pageoffset=" pageoffset";";
- }
- ' $3$SYNC >> $OUT
- ### Step 3
- awk '
- BEGIN { state=11; }
- /Output/ { state=12; }
- /Content/ { state=11; }
- /!/ { if (state==12) { print "!xxx"; next; } }
- { if (state==11){next}; print; }
- ' $1.0$SYNC >> $OUT
- ### Step 4
- #input=inputoffset
- awk -v input=70 -v pagefrom_1=2 '
- BEGIN { FS=":"; state=20; page=0; pagefrom[1]=pagefrom_1; }
- /Content/ { state=21; }
- /Input/ { if (state == 21) {
- input+=1;
- print "Input:"input":"$3;
- next;
- }
- }
- /Postamble/ { state=22; }
- /\{/ { if (state == 21) {
- page+=1;
- if (pagefrom[1] <= page) { state=22; }
- }
- }
- /!/ { if (state == 21) { print "!xxx"; next; } }
- { if ((state==20)||(state==22)) { next }; print; }
- END { inputoffset=input; print "---- inputoffset="inputoffset";"; }
- ' $1.0$SYNC >> $OUT
- #page=pageoffset=pagefrom[incl]
- awk -v inputoffset=100 -v page=1 '
- BEGIN { FS=":"; state=22; }
- /Content/ { state=23; }
- /Input/ { if (state == 24) {
- input+=1;
- print "Input:" $2+inputoffset ":" $3;
- next;
- }
- }
- /Postamble/ { state=22; }
- /\{/ { FS=",";
- if (state == 24) {
- page+=1; s="{"page; print s; next;
- }
- }
- /\}/ { FS=":"; s="}"page; print s; next; }
- /\[/ { if (state == 24) {
- s="[" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /\(/ { if (state == 24) {
- s="(" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /v/ { if (state == 24) {
- s="v" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /h/ { if (state == 24) {
- s="h" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /x/ { if (state == 24) {
- s="x" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /k/ { if (state == 24) {
- s="k" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /g/ { if (state == 24) {
- s="g" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /\$/ { if (state == 24) {
- s="$" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /!/ { if (state == 23) { state=24; next; };
- if (state == 24) { print "!xxx"; next; } }
- { if ((state == 22)||(state == 23)) { next }; print; }
- ' $2$SYNC >> $OUT
- #page=pageoffset
- awk -v inputoffset=200 -v page=3 '
- BEGIN { FS=":"; state=22; }
- /Content/ { state=23; }
- /Input/ { if (state == 24) {
- input+=1;
- print "Input:" $2+inputoffset ":" $3;
- next;
- }
- }
- /Postamble/ { state=22; }
- /\{/ { FS=",";
- if (state == 24) { page+=1; s="{"page; print s; next; }
- }
- /\}/ { FS=":"; s="}"page; print s; next; }
- /\[/ { if (state == 24) {
- s="[" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /\(/ { if (state == 24) {
- s="(" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /v/ { if (state == 24) {
- s="v" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /h/ { if (state == 24) {
- s="h" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /x/ { if (state == 24) {
- s="x" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /k/ { if (state == 24) {
- s="k" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /g/ { if (state == 24) {
- s="g" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /\$/ { if (state == 24) {
- s="$" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
- };
- }
- /!/ { if (state == 23) { state=24; next; };
- if (state == 24) { print "!xxx"; next; } }
- { if ((state == 22)||(state == 23)) { next }; print; }
- ' $3$SYNC >> $OUT
- ### Step 5
- awk -v inputoffset=400 '
- BEGIN { FS=":"; state=30; input=inputoffset; }
- /Input/ { if (state == 0) {
- input+=1;
- print "Input:"input":"$3;
- next;
- }
- }
- /Postamble/ { state=31; }
- /!/ { if (state==31) { print "!xxx"; next; } }
- { if (state==30){next}; print; }
- ' $1.0$SYNC >> $OUT
- ### Step 6
- awk '
- BEGIN { p=0; lastp=0; }
- /!/ { s="!"p-lastp; lastp=p; print s; p+=length(s)+1; next; }
- /----/ { next; }
- { print; p+=length($0)+1; }
- ' $OUT | gzip > $1$SYNCGZ
- #remove comment to delete temporary files
- # rm -f "$OUT" "$1.0$SYNC" "$1.0$SYNCGZ"
- # for SFILE; do
- # rm -f "$SFILE$SYNC"
- # done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement