AkelPad Forum Index AkelPad
Support forum
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Request for help: Converting a small script into Akelpad

 
Post new topic   Reply to topic    AkelPad Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
wickyd



Joined: 24 Apr 2011
Posts: 7

PostPosted: Sun May 06, 2012 8:07 am    Post subject: Request for help: Converting a small script into Akelpad Reply with quote

Hello,

I have a script in NoteTab Light that I would like to convert into a script that I can use inside Akelpad.

What the script does:

I can write a few lines of text, such as:

List item 1
List item 2
List item 3

I then highlight the text, double-click on the name of this clip (left-side pane list of scripts called clips) and it reformats the text to look like this:

[LIST]
[*]List item 1
[*]List item 2
[*]List item 3
[/LIST]

Here is the script:

^!SetListDelimiter ^P
^!SetArray %Array%=^$GetSelection$
^!Set %Increment%=1
^!InsertText [LIST]
:StartLoop
^!If ^$StrSize(^%Array^%Increment%%)$ > 0 ^!Goto PrintLine ELSE ^!Goto Finish
:PrintLine
^!InsertText ^P[*]^%Array^%Increment%%
^!Inc %Increment%
^!Goto StartLoop
:Finish
^!InsertText ^P[/LIST]

Any help would be greatly appreciated.
Back to top
View user's profile Send private message
Infocatcher



Joined: 06 Aug 2007
Posts: 1562

PostPosted: Sun May 06, 2012 9:32 am    Post subject: Reply with quote

Scripts plugin +
bbCodeList.js
Code:
AkelPad.ReplaceSel(
   "[list]\n"
   + AkelPad.GetSelText()
      .replace(/^/mg, "[*]")
   + "\n[/list]"
);


Or ignore empty lines:
Code:
AkelPad.ReplaceSel(
   "[list]\n"
   + AkelPad.GetSelText()
      .replace(/^[ \t]*\S/mg, "[*]$&")
   + "\n[/list]"
);
Back to top
View user's profile Send private message
FeyFre



Joined: 07 Aug 2007
Posts: 2004
Location: Vinnitsa, Ukraine

PostPosted: Sun May 06, 2012 9:53 am    Post subject: Reply with quote

Code:
AkelPad.ReplaceSel("[LIST]\n[*]"+AkelPad.GetSelText().split("\r").join("\n[*]")+"\n[/LIST]");
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
VladSh



Joined: 29 Nov 2006
Posts: 2584
Location: Киев, Русь

PostPosted: Thu Oct 04, 2012 9:30 am    Post subject: Reply with quote

FeyFre, Infocatcher
Здорово, но не совсем.
Если текст идёт со сдвигом:
Code:
выодпл
   строка-1
   строка-2
   строка-3
sdgasdf
то вот такое:
Code:
выодпл
   [LIST]
   [*]строка-1
   [*]строка-2
   [*]строка-3
   [/LIST]
sdgasdf
с вышеуказанным кодом не получится.
Back to top
View user's profile Send private message Visit poster's website
FeyFre



Joined: 07 Aug 2007
Posts: 2004
Location: Vinnitsa, Ukraine

PostPosted: Thu Oct 04, 2012 10:12 am    Post subject: Reply with quote

VladSh, навскидку.
Code:
//! Только если JScript ещё не поддерживает этот итератор
if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

var Arr = AkelPad.GetSelText().split("\r");
Arr.forEach(function(el,ind,ar){ar[ind]=el.replace(/^(\s*)(.*)$/,"$1[*]$2");});
AkelPad.ReplaceSel("[LIST]\n"+Arr.join("\n")+"\n[/LIST]");
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Infocatcher



Joined: 06 Aug 2007
Posts: 1562

PostPosted: Thu Oct 04, 2012 10:26 am    Post subject: Reply with quote

FeyFre
Тут больше Array.map по логике подходит. Smile


Дополненный вариант с «ignore empty lines»:
Code:
AkelPad.ReplaceSel(
   "[list]\n"
   + AkelPad.GetSelText()
      .replace(/^([ \t]*)(\S)/mg, "$1[*]$2")
   + "\n[/list]"
);

С отступами перед [list] и [/list]:
Code:
var sel = AkelPad.GetSelText();
var spaces = sel.match(/^[ \t]*/)[0];
AkelPad.ReplaceSel(
   spaces + "[list]\n"
   + sel.replace(/^([ \t]*)(\S)/mg, "$1[*]$2")
   + "\n" + spaces + "[/list]"
);
Back to top
View user's profile Send private message
VladSh



Joined: 29 Nov 2006
Posts: 2584
Location: Киев, Русь

PostPosted: Thu Oct 04, 2012 11:49 am    Post subject: Reply with quote

FeyFre, Infocatcher
Спасибо!

Я тут походу спрашиваю, т.к. хотел бы изменить свой скрипт XMLStructure.js.
Предыдущий вариант от FeyFre был удобнее, и я его уже перепилил на переменные (а в будущем на получение параметров):
Code:
var rootOpen = "[LIST]";
var lineOpen = "\t[*]";
var lineClose = "[#]";
var rootClose = "[/LIST]";
AkelPad.ReplaceSel(rootOpen + "\n" + lineOpen + AkelPad.GetSelText().split("\r").join(lineClose + "\n" + lineOpen) + lineClose + "\n" + rootClose);
но вышел облом со сдвигом.

2-й же вариант - почти то же самое, что и у меня.
Теперь вариант Infocatcher'а более экономичен. Посмотрю, подходит ли он для тюнинга, если нет, то оставлю как есть. Не совсем понятно, правда, как lineClose сюда всобачить..
Back to top
View user's profile Send private message Visit poster's website
FeyFre



Joined: 07 Aug 2007
Posts: 2004
Location: Vinnitsa, Ukraine

PostPosted: Thu Oct 04, 2012 11:55 am    Post subject: Reply with quote

Infocatcher, map, может быть. Но в нужном направлении пнул.
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
VladSh



Joined: 29 Nov 2006
Posts: 2584
Location: Киев, Русь

PostPosted: Thu Oct 04, 2012 11:56 am    Post subject: Reply with quote

Что-то типа того:
Code:
var rootOpen = "[list]";
var lineOpen = "\t[*]";
var lineClose = "[#]";
var rootClose = "[/list]";

var sel = AkelPad.GetSelText();
var spaces = sel.match(/^[ \t]*/)[0];

var lines = sel.replace(/^([ \t]*)(\S)/mg, "$1" + lineOpen + "$2").replace("\r", lineClose + "\r") + lineClose;
if (!sel)
   lines = lineOpen + lines;

AkelPad.ReplaceSel(spaces + rootOpen + "\n" + lines + "\n" + spaces + rootClose);

Добавлено: переделал в insertStructure.js.
Infocatcher, FeyFre, спасибо!
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AkelPad Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


SourceForge.net Logo Powered by phpBB © 2001, 2005 phpBB Group