[texworks] several replacements

Paul A Norman paul.a.norman at gmail.com
Sat Feb 18 13:15:40 CET 2012


Hi GM,

No worries ... I'm afraid that the English language can be a little
ambiguous at times " I need your help to write a script that would " could
imply both scenarios.

What you have done looks great.

You could try encapsulating the list of things to do as an array, or even
better perhaps  as an external text file that you can edit separately from
your script if you need to change or add items.

An external text file, say,  "*myReplacements.txt*" could look similar to
what you first indicated if you wished (but with only one space be fore and
after our -> ), write/edit it in TeXworks so we get the correct
line-breaks. Save it into the same directory as your script.

something[space]->[space]somethingElse

\text{A} ->  A
\text{B} ->  B
\text{i} ->  \ii
\text{e} ->  \e
\Ouv ->  \ouv

  We'll get script to handle the \escaping, so that you/your Users can just
write plain text with no extra backslashes required in *myReplacements.txt *
*
*
*  I have not thought through all that needs to be in** function
escapeNeedle()* - its a bit late at night here now! So you may need to add
anything else that would interfere with a regular expression.


//====== Script ======

function escapeNeedle(needle)

{

 needle = needle.replace(/\\/g, "\\\\");

  needle = needle.replace(/\{/g, "\\{");

  needle = needle.replace(/\}/g, "\\}");

    needle = needle.replace(/\[/g, "\\[");

    needle = needle.replace(/\]/g, "\\]");


// and others as needed


return needle;

}


   function altertext(txt)

{

for (count in myReplacementList)

{

   var actionThis = myReplacementList[count].split(" -> "); // 0 = find, 1
= replace with



      actionThis[0] = escapeNeedle(actionThis[0]);


  var regExprsn = new RegExp(actionThis[0], "g");

      // TW.information(null,"",regExprsn);


  txt = txt.replace(regExprsn, actionThis[1]);

 }


 return txt;

}


  // this will give us an array element for each line in file

// assuming file has been set, or script is pointless

var myReplacementList =
TW.readFile("myReplacements.txt").result.split("\n");


// could split above into steps and check for status first see
http://twscript.paulanorman.com/docs/html/TexWorksQtScripting.html?readFile.html



var txt = TW.target.selection;

var pos = TW.target.selectionStart;


if (myReplacementList == false) // file is probably empty

 {

  TW.information(null,"","Please Create and populate myReplacements.txt in
Script's folder.\n\n"

+" Format:\nsomething[space]->[space]somethingElse\ne.g.\n"

+"\\text{A} -> A");

}

 else

  if (txt != "")

   {

     txt = altertext(txt);

     TW.target.insertText(txt);

    TW.target.selectRange(pos, txt.length);

   }

else

{

  TW.information(null,"","Please Select Some text First");

}


//===== End Script ====


Hope that this helps, thanks I'll probably use it myself from time to time!


Paul

On 18 February 2012 22:06, GM <gm at marris.fr> wrote:

> Le 18/02/2012 02:16, Paul A Norman a écrit :
>
>> Hi GM,
>>
>
> Bonjour,
>
>
>  Have you done some scripting before
>>
>
> No, but I still tried to understand ... and it seems that this is what I
> asked.
>
> Did I not write too much nonsense?
>
> Thanks
> GM
>
> ===========
> // TeXworksScript
> // Title:  ReplaceByGM
> // Description: Nettoyage du texte
> // Author:  G.M.
> // Version: 0.1
> // Date: 2012-02-18
> // Script-Type: standalone
> // Context: TeXDocument
> // Shortcut: Alt+E, Alt+R
>
> String.prototype.replacebyGM = function() {
>    var x=this.toString();
>    x=x.replace(/\\text\{A\}/g,"A"**);
>    x=x.replace(/\\text\{B\}/g,"B"**);
>    x=x.replace(/\\text\{C\}/g,"C"**);
>    x=x.replace(/\\text\{D\}/g,"D"**);
>    x=x.replace(/\\text\{E\}/g,"E"**);
>    x=x.replace(/\\text\{e\}/g,"\\**e");
>    x=x.replace(/\\text\{i\}/g,"\\**ii");
>    return x;
> };
>
> var txt = TW.target.selection;
> if (txt != "") {
>  var pos = TW.target.selectionStart;
>  txt = txt.replacebyGM();
>  TW.target.insertText(txt);
>  TW.target.selectRange(pos, txt.length);
> }
>
> undefined;
> ===========
>
>
>
>
>> Paul
>>
>> On 18 February 2012 08:38, GM<gm at marris.fr>  wrote:
>>
>>  Hello,
>>>
>>> I need your help to write a script that would perform several
>>> replacements
>>> in a tex file.
>>>
>>> ===================
>>> \text{A} ->  A
>>> \text{B} ->  B
>>> ...
>>> \text{i} ->  \ii
>>> \text{e} ->  \e
>>> \Ouv ->  \ouv
>>> ...
>>> (1 tab) ->  (4 spaces)
>>> etc.. etc.
>>> ===================
>>>
>>> Thank you to specialists javascript to tell me how I could do that.
>>>
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://tug.org/pipermail/texworks/attachments/20120219/03b5150d/attachment.html>


More information about the texworks mailing list