<http://forum.pspad.com/read.php?f=2&i=11680&t=11680> 
----------------------------------------------------------------

Hi All

Further to my suggestion for a feature (see this thread:
http://forum.pspad.com/read.php?f=2&i=11511&t=11511), I went about
creating a script to implement it. Copy the following into a Javascript
document and save to PSPad\Script\JScript\AutoInclude.js. Your
comments, bug reports and suggestions are welcome:

module_name = "HTML AutoInclude";
module_ver = "0.1";
// Code copyright 2006 T, Pattison. Please contact me via
// geniscope.atspace.com with any ammendments or enhancements to this
// script.

// This script updates the contents of the current HTML file with matching
// blocks within the "include.html" file.
// This script expects a file called "include.html" to be present within
// the open project.
// Blocks are identified by pairs of HTML comments:
// <!-- name -->[newline]X number of Lines of HTML[newline]<!-- Endname
-->
// Script takes named comment from include.html and searches for the same
// comment within the current file.

function GetBlocks(){
  logwin = NewEditor();
  logwin.AssignLog();
  var editor = newEditor();
  editor.assignActiveEditor();
  curFile=editor.fileName();
  logwin.appendText("Updating: "+curFile+"\n");
  var obj2 = newEditor();
  obj2.openFile("include.html");
  obj2.command("ecNormalSelect");
  iText = obj2.Text();
  ilines = iText.split("\n");
  var inBlk=0;
  for (var ii=0; ii<ilines.length; ii++) {
    if (inBlk==0) { 
    obj2.setCaretPos(0,ii+1);   
      if (ilines[ii].indexOf("<!-- ")>=0) {
        if (ilines[ii].indexOf("<!-- End")==-1) {
          var cmtEnd=ilines[ii].indexOf("->");
          Blk=ilines[ii].substring(5,(cmtEnd-2));
          logwin.appendText("Finding: *"+Blk+"*"+"\n");
          inBlk=1;
        }
        else {logwin.appendText("Error in include.html\n");}
      }
    }
    if (inBlk==1) {
      obj2.command("ecSelDown");    
      if (ilines[ii].indexOf("<!-- End"+Blk)>=0) {
        inBlk=0;
        obj2.command("ecSelUp");
        //ready to copy
        obj2.command("ecCopy");
        GotoMark(Blk);
      }
    }
  }
  if (inBlk==1){logwin.appendText("Error in include.html: file ends
before end comment found\n");}
  obj2 = null;
  logwin.appendText("Done!\n");
}

function GotoMark(Blk){
    pattern = "<!-- "+Blk+" -->";
    stPos = GetStrPos(pattern);
    pattern = "<!-- End"+Blk+" -->";
    enPos = GetStrPos(pattern);
    var editor = newEditor();
    editor.assignEditorByName(curFile);
    editor.command("ecLineStart");    
    editor.command("ecNormalSelect");
    if(enPos>stPos && stPos>=0 && enPos>=0){
        editor.setCaretPos(0,stPos);
        for (var kk=0; kk<(enPos-stPos); kk++) {
            editor.command("ecSelDown");
        }
        editor.command("ecPaste");
    }
    else{
        logwin.appendText("No match found for "+Blk+"\n");
    }
    editor = null;
}

function GetStrPos(pattern){
    var editors = newEditor();
    editors.assignEditorByName(curFile);
    allText = editors.Text();
    lines = allText.split("\n");
    var iPos=0;
    var StrPosn = -1;
    for (var jj=0; jj<lines.length; jj++) {
        iPos = lines[jj].indexOf(pattern);
        if(iPos != -1){
            StrPosn = jj+1;
            logwin.appendText("Valid comment found at line
"+StrPosn+"\n");
            return (StrPosn);
            break;
        }
    }
    editors = null;
}

function Init(){
  addMenuItem("Update Include", "HTML AutoInclude", "GetBlocks");
}

-- 
PSPad freeware editor http://www.pspad.com

Reply via email to