String fileName = "" Boolean sortByTextAppearance = false, argsOk=false String errorText="Usage groovy OrderFootnotes.groovy [option] filename\noption: -t: order footnotes by text appearance" if( args ){ if( args.size() == 2 ){ argsOk=true args.each{ String arg -> if( arg=="-t" ){ sortByTextAppearance = true } else { fileName = arg } } } else if( args.size() == 1 ){ argsOk=true fileName = args[0] } } if( ! argsOk ){ println errorText return } Map mapOldNewNumber = [:] FileReader fr = new FileReader(fileName) Map footnoteNumbers = [:] Set footnoteSet = [] if( sortByTextAppearance ){ if(fr.ready()){ Boolean isFootnotes = false long newCount = 0 fr.eachLine{ String line -> String parsed = "" Long startIndex = 0 line.eachMatch(/\[[0-9]+\]/){ number -> long tmpNumber = Long.valueOf(number[0].replaceAll(/[\[\]]/,"")) if( isFootnotes ){ footnoteNumbers[mapOldNewNumber[tmpNumber]] = line - number[0] } else { long tmpCount = mapOldNewNumber[tmpNumber] if( ! tmpCount ){ tmpCount = mapOldNewNumber[tmpNumber] = ++newCount } parsed += line[startIndex.. if( isFootnotes ){ line.eachMatch(/\[[0-9]+\]/){ number -> long tmpNumber = Long.valueOf(number[0].replaceAll(/[\[\]]/,"")) footnoteNumbers["${++newCount}"] = line - number[0] mapOldNewNumber["${tmpNumber}"] = newCount } } if ( line ==~ /@footnote:/ && ! isFootnotes ) isFootnotes = true } fr.close() fr = new FileReader(fileName) isFootnotes = false fr.eachLine{ String line -> if ( line ==~ /@footnote:/ && ! isFootnotes ) { isFootnotes = true println line } if( ! isFootnotes ){ Long startIndex = 0 String parsed = "" line.eachMatch(/\[[0-9]+\]/){ number -> long tmpNumber = Long.valueOf(number[0].replaceAll(/[\[\]]/,"")) long replaceNum = mapOldNewNumber["$tmpNumber"] parsed += line[startIndex..