Request for help: Converting a small script into Akelpad

Discuss and announce AkelPad plugins
Post Reply
  • Author
  • Message
Offline
Posts: 7
Joined: Sun Apr 24, 2011 8:18 pm

Request for help: Converting a small script into Akelpad

Post by wickyd »

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.

Offline
Posts: 1862
Joined: Mon Aug 06, 2007 1:07 pm
Contact:

Post by Infocatcher »

Scripts plugin +
bbCodeList.js

Code: Select all

AkelPad.ReplaceSel(
	"[list]\n"
	+ AkelPad.GetSelText()
		.replace(/^/mg, "[*]")
	+ "\n[/list]"
);
Or ignore empty lines:

Code: Select all

AkelPad.ReplaceSel(
	"[list]\n"
	+ AkelPad.GetSelText()
		.replace(/^[ \t]*\S/mg, "[*]$&")
	+ "\n[/list]"
);

Offline
Posts: 2247
Joined: Tue Aug 07, 2007 2:03 pm
Location: Vinnitsa, Ukraine

Post by FeyFre »

Code: Select all

AkelPad.ReplaceSel("[LIST]\n[*]"+AkelPad.GetSelText().split("\r").join("\n[*]")+"\n[/LIST]");

Offline
Posts: 3217
Joined: Wed Nov 29, 2006 1:19 pm
Location: Киев, Русь
Contact:

Post by VladSh »

FeyFre, Infocatcher
Здорово, но не совсем.
Если текст идёт со сдвигом:

Code: Select all

выодпл
   строка-1
   строка-2
   строка-3
sdgasdf
то вот такое:

Code: Select all

выодпл
   [LIST]
   [*]строка-1
   [*]строка-2
   [*]строка-3
   [/LIST]
sdgasdf
с вышеуказанным кодом не получится.

Offline
Posts: 2247
Joined: Tue Aug 07, 2007 2:03 pm
Location: Vinnitsa, Ukraine

Post by FeyFre »

VladSh, навскидку.

Code: Select all

//! Только если 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]");

Offline
Posts: 1862
Joined: Mon Aug 06, 2007 1:07 pm
Contact:

Post by Infocatcher »

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


Дополненный вариант с «ignore empty lines»:

Code: Select all

AkelPad.ReplaceSel(
	"[list]\n"
	+ AkelPad.GetSelText()
		.replace(/^([ \t]*)(\S)/mg, "$1[*]$2")
	+ "\n[/list]"
);
С отступами перед [list] и [/list]:

Code: Select all

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]"
);

Offline
Posts: 3217
Joined: Wed Nov 29, 2006 1:19 pm
Location: Киев, Русь
Contact:

Post by VladSh »

FeyFre, Infocatcher
Спасибо!

Я тут походу спрашиваю, т.к. хотел бы изменить свой скрипт XMLStructure.js.
Предыдущий вариант от FeyFre был удобнее, и я его уже перепилил на переменные (а в будущем на получение параметров):

Code: Select all

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 сюда всобачить..

Offline
Posts: 2247
Joined: Tue Aug 07, 2007 2:03 pm
Location: Vinnitsa, Ukraine

Post by FeyFre »

Infocatcher, map, может быть. Но в нужном направлении пнул.

Offline
Posts: 3217
Joined: Wed Nov 29, 2006 1:19 pm
Location: Киев, Русь
Contact:

Post by VladSh »

Что-то типа того:

Code: Select all

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, спасибо!
Post Reply