Advertisement
e-birk

synctex-inclpdf-alpha.sh

May 26th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.33 KB | None | 0 0
  1. #!/bin/sh
  2. # File: synctex-inclpdf-alpha.sh
  3. # License: LPPL, version 1.3 or later
  4. # Version: 0.0.0.1
  5. # Date: 26-05-2014
  6. # Author: Birk Eisermann
  7.  
  8. if [ $# -ne 3 ]; then
  9.     echo ""
  10.     echo "usage:  $0 'main' 'incl_1' 'incl_2' ..."
  11.     echo "        'main'     filename of the main file without extension,"
  12.     echo "                      e.g. 'combined' for 'combined.synctex.gz'"
  13.     echo "        'incl_N' filename of the N-th included pdf without extension,"
  14.     echo "                     e.g. 'subdir/./incl1' for 'subdir/./incl1.synctex.gz'"
  15.     echo ""
  16.     echo "Note: This is a first draft where certain things are hard coded."
  17.     echo "Requirements:"
  18.     echo " - one main file with 1 page followed by includepdf's."
  19.     echo " - exactly *two* includepdf with each having exactly two pages."
  20.     echo "You can change the content of a page but due to the re-boxing by"
  21.     echo "the package pdfpages the inverse search can be off by several lines;"
  22.     echo "Nevertheless the page number should be correct."
  23.     exit 1
  24. fi
  25.  
  26.  
  27. ### Step 1
  28. SYNCGZ=.synctex.gz
  29. SYNC=.synctex
  30. for SFILE; do   # unzipping all files + save main file
  31.     gunzip -c $SFILE$SYNCGZ > $SFILE$SYNC
  32. done
  33. mv $1$SYNC $1.0$SYNC
  34. mv $1$SYNCGZ $1.0$SYNCGZ
  35.  
  36. OUT="$1.1$SYNC"
  37.  
  38. #returns: jmain, inputoffset, maxpages(not used yet)
  39. awk -v root=$1 '
  40. BEGIN       { FS=":"; state=0; input=0; maxpages=0; jmain=-1;
  41.              texfilename=root ".tex";
  42.            }
  43. /Input/     { if (state == 0) {
  44.                input+=1;
  45.                print "Input:" input ":" $3;
  46.                if (index($3, texfilename) > 0) { jmain=input; }
  47.                next;
  48.              }
  49.            }
  50. /Output/    { state=1; }
  51. /\{/        { maxpages+=1; }
  52.            { if (state==1){next}; print; }
  53. END         { inputoffset=input;
  54.              print "---- inputoffset=" inputoffset "; jmain=" jmain "; maxpages=" maxpages ";";
  55.            }
  56. ' $1.0$SYNC > $OUT
  57.  
  58.  
  59. ### Step 2
  60. #returns arrays index by incl: inputfrom, inputto, pagefrom, pageto, texinput
  61. #                     by input: inputfiles
  62. awk -v inclfilename=$2 -v incl=1 -v inputoffset=100 -v pageoffset=1 '
  63. BEGIN       { FS=":"; state=10; input=0; page=0;
  64.              filename=FILENAME; texfilename=inclfilename ".tex"; texinput[incl]=-1;
  65.              print "---- texfilename="texfilename;
  66.            }
  67. /Input/     { if (state == 10) {
  68.                input+=1;
  69.                inputfiles[inputoffset+input]=$3;
  70.                if (index($3, texfilename) > 0) {
  71.                   texinput[incl]=inputoffset+input;
  72.                }
  73.                print "Input:" inputoffset+input ":" $3;
  74.                next;
  75.              }
  76.            }
  77. /Output/    { state=11; }
  78. /\{/        { page+=1; }
  79.            { }
  80. END         { inputfrom[incl]=inputoffset+1;
  81.              inputto[incl]=inputoffset+input;
  82.              inputoffset=inputto[incl];
  83.              pagefrom[incl]=pageoffset+1;
  84.              pageto[incl]=pageoffset+page;
  85.              pageoffset=pageto[incl];
  86.              print "---- inputfrom["incl"]=" inputfrom[incl] ", inputto["incl"]=" inputto[incl] ";";
  87.              print "---- pagefrom["incl"]=" pagefrom[incl] ", pageto["incl"]=" pageto[incl] ";"
  88.              print "---- texinput["incl"]=" texinput[incl] ";";
  89.              print "---- inputfiles["inputto[incl]"]=" inputfiles[inputto[incl]] ";";
  90.              print "---- inputoffset=" inputoffset ", pageoffset=" pageoffset";";
  91.            }
  92. ' $2$SYNC >> $OUT
  93.  
  94. awk -v inclfilename=$3 -v incl=2 -v inputoffset=200 -v pageoffset=3 '
  95. BEGIN       { FS=":"; state=10; input=0; page=0;
  96.              filename=FILENAME; texfilename=inclfilename ".tex"; texinput[incl]=-1;
  97.              print "---- texfilename="texfilename;
  98.            }
  99. /Input/     { if (state == 10) {
  100.                input+=1;
  101.                inputfiles[inputoffset+input]=$3;
  102.                if (index($3, texfilename) > 0) {
  103.                   texinput[incl]=inputoffset+input;
  104.                }
  105.                print "Input:" inputoffset+input ":" $3;
  106.                next;
  107.              }
  108.            }
  109. /Output/    { state=11; }
  110. /\{/        { page+=1; }
  111.            { }
  112. END         { inputfrom[incl]=inputoffset+1;
  113.              inputto[incl]=inputoffset+input;
  114.              inputoffset=inputto[incl];
  115.              pagefrom[incl]=pageoffset+1;
  116.              pageto[incl]=pageoffset+page;
  117.              pageoffset=pageto[incl];
  118.              print "---- inputfrom["incl"]=" inputfrom[incl] ", inputto["incl"]=" inputto[incl] ";";
  119.              print "---- pagefrom["incl"]=" pagefrom[incl] ", pageto["incl"]=" pageto[incl] ";"
  120.              print "---- texinput["incl"]=" texinput[incl] ";";
  121.              print "---- inputfiles["inputto[incl]"]=" inputfiles[inputto[incl]] ";";
  122.              print "---- inputoffset=" inputoffset ", pageoffset=" pageoffset";";
  123.            }
  124. ' $3$SYNC >> $OUT
  125.  
  126. ### Step 3
  127. awk '
  128. BEGIN       { state=11; }
  129. /Output/    { state=12; }
  130. /Content/   { state=11; }
  131. /!/         { if (state==12) { print "!xxx"; next; } }
  132.            { if (state==11){next}; print; }
  133. ' $1.0$SYNC >> $OUT
  134.  
  135.  
  136. ### Step 4
  137. #input=inputoffset
  138. awk -v input=70 -v pagefrom_1=2 '
  139. BEGIN       { FS=":"; state=20; page=0; pagefrom[1]=pagefrom_1; }
  140. /Content/   { state=21; }
  141. /Input/     { if (state == 21) {
  142.                input+=1;
  143.                print "Input:"input":"$3;
  144.                next;
  145.              }
  146.            }
  147. /Postamble/ { state=22; }
  148. /\{/         { if (state == 21) {
  149.                page+=1;
  150.                if (pagefrom[1] <= page) { state=22; }
  151.              }
  152.            }
  153. /!/         { if (state == 21) { print "!xxx"; next; } }
  154.            { if ((state==20)||(state==22)) { next }; print; }
  155. END         { inputoffset=input; print "---- inputoffset="inputoffset";"; }
  156. ' $1.0$SYNC >> $OUT
  157.  
  158. #page=pageoffset=pagefrom[incl]
  159. awk  -v inputoffset=100 -v page=1 '
  160. BEGIN       { FS=":"; state=22; }
  161. /Content/   { state=23; }
  162. /Input/     { if (state == 24) {
  163.                input+=1;
  164.                print "Input:" $2+inputoffset ":" $3;
  165.                next;
  166.              }
  167.            }
  168. /Postamble/ { state=22; }
  169. /\{/        { FS=",";
  170.              if (state == 24) {
  171.                page+=1; s="{"page; print s; next;
  172.              }
  173.            }
  174. /\}/        { FS=":"; s="}"page; print s; next; }
  175. /\[/        { if (state == 24) {
  176.                  s="[" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  177.              };
  178.            }
  179. /\(/        { if (state == 24) {
  180.                  s="(" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  181.              };
  182.            }
  183. /v/         { if (state == 24) {
  184.                  s="v" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  185.              };
  186.            }
  187. /h/         { if (state == 24) {
  188.                  s="h" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  189.              };
  190.            }
  191. /x/         { if (state == 24) {
  192.                  s="x" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  193.              };
  194.            }
  195. /k/         { if (state == 24) {
  196.                  s="k" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  197.              };
  198.            }
  199. /g/         { if (state == 24) {
  200.                  s="g" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  201.              };
  202.            }
  203. /\$/        { if (state == 24) {
  204.                  s="$" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  205.              };
  206.            }
  207. /!/         { if (state == 23) { state=24; next; };
  208.              if (state == 24) { print "!xxx"; next; } }
  209.            { if ((state == 22)||(state == 23)) { next }; print; }
  210. ' $2$SYNC >> $OUT
  211.  
  212. #page=pageoffset
  213. awk  -v inputoffset=200 -v page=3 '
  214. BEGIN       { FS=":"; state=22; }
  215. /Content/   { state=23; }
  216. /Input/     { if (state == 24) {
  217.                input+=1;
  218.                print "Input:" $2+inputoffset ":" $3;
  219.                next;
  220.              }
  221.            }
  222. /Postamble/ { state=22; }
  223. /\{/        { FS=",";
  224.              if (state == 24) { page+=1; s="{"page; print s; next; }
  225.            }
  226. /\}/        { FS=":"; s="}"page; print s; next; }
  227. /\[/        { if (state == 24) {
  228.                  s="[" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  229.              };
  230.            }
  231. /\(/        { if (state == 24) {
  232.                  s="(" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  233.              };
  234.            }
  235. /v/         { if (state == 24) {
  236.                  s="v" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  237.              };
  238.            }
  239. /h/         { if (state == 24) {
  240.                  s="h" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  241.              };
  242.            }
  243. /x/         { if (state == 24) {
  244.                  s="x" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  245.              };
  246.            }
  247. /k/         { if (state == 24) {
  248.                  s="k" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  249.              };
  250.            }
  251. /g/         { if (state == 24) {
  252.                  s="g" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  253.              };
  254.            }
  255. /\$/        { if (state == 24) {
  256.                  s="$" substr($1,2)+inputoffset substr($0,length($1)+1); print s; next;
  257.              };
  258.            }
  259. /!/         { if (state == 23) { state=24; next; };
  260.              if (state == 24) { print "!xxx"; next; } }
  261.            { if ((state == 22)||(state == 23)) { next }; print; }
  262. ' $3$SYNC >> $OUT
  263.  
  264.  
  265. ### Step 5
  266. awk -v inputoffset=400 '
  267. BEGIN       { FS=":"; state=30; input=inputoffset; }
  268. /Input/     { if (state == 0) {
  269.                input+=1;
  270.                print "Input:"input":"$3;
  271.                next;
  272.              }
  273.            }
  274. /Postamble/ { state=31; }
  275. /!/         { if (state==31) { print "!xxx"; next; } }
  276.            { if (state==30){next}; print; }
  277. ' $1.0$SYNC >> $OUT
  278.  
  279.  
  280. ### Step 6
  281. awk '
  282. BEGIN       { p=0; lastp=0; }
  283. /!/         { s="!"p-lastp; lastp=p; print s; p+=length(s)+1; next; }
  284. /----/      { next; }
  285.            { print; p+=length($0)+1; }
  286. ' $OUT | gzip > $1$SYNCGZ
  287.  
  288. #remove comment to delete temporary files
  289. # rm -f "$OUT" "$1.0$SYNC" "$1.0$SYNCGZ"
  290. # for SFILE; do
  291. #     rm -f "$SFILE$SYNC"
  292. # done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement