~dmbaturin/soupault

5 3

selectors.lua allows multiple selectors

Details
Message ID
<8e7bed03-a1d3-2d54-4780-cdf2e5fdc2b5@aoirthoir.com>
DKIM signature
pass
Download raw message
I have worked on several functions and plugins over the last week. I 
will be cleaning them up and working on a couple more tomorrow. Then I 
will post them to my website if I am not driving (I am a truck driver).

One is selectors.lua which is a function to return an array of multiple 
comma separated selectors rather than just a single selector. The 
selectors separation, by using commas, means you can use normal css 
selector rules and viola!

This is the function code:

function selectors()
   selectors = config["selectors"]
   if not selectors then
     Plugin.exit("No selectors chosen")
   else
     selectors = Regex.split(selectors,",")
   end
   countselectors = size(selectors)
   return selectors, countselectors
end

To use the function you would either just copy the function to your 
plugin or access it with dofile. I will be creating all my plugins and 
functions in a directory:

yoursoupault project/add1tocobol/functions (for functions)

yoursoupault project/add1tocobol/plugins (for plugins)

So when I have the plugins/functions on my website you will be able to 
download then unzip a file to your soupault project and it will just 
create the add1tocobol directory. In which case the dofile command is thus:

dofile("add1tocobol/functions/selectors.lua")

so to use you do something like this in your plugin

dofile("add1tocobol/functions/selectors.lua")
selectors, selectorcount = selectors()

while (indexselector <= countselectors) do
   selector = selectors[indexselector]
   elements = HTML.select(page, selector)
   count = size(elements)
   index = 1
   while (index <= count) do
      element = elements[index]
      SOME CODE
      index = index + 1
   end
   indexselector = indexselector + 1
end

I will be moving the seletors, selectorcount = selectors() command 
directly into the selectors.lua so they will automatically be created. I 
think a discussion on the best sorta global names for these might be 
useful? right now there are things like page HTML.this and that ...etc.. 
but for now its just selectors...

ive not read up on the namespacing in lua so i am not sure how to mimic 
the HTML.something with say ADD1.something so that my globals dont 
conflict with other plugin globals... i'd not mind it being 
ADD1.selectors just to make things easier for everyone to prevent 
conflicts...

I also have an apply.lua that just checks to see the value of 
config["apply"]... will be adding functions that determine the value of 
page_include, page_exclude, section_include, section_exclude... so that 
the functions can be called before anything else and if the page isnt 
excluded or not included... the plugin.exit can be called...
Details
Message ID
<55114164-7f2b-84b9-d27d-e221e610abc4@aoirthoir.com>
In-Reply-To
<8e7bed03-a1d3-2d54-4780-cdf2e5fdc2b5@aoirthoir.com> (view parent)
DKIM signature
pass
Download raw message
ok well it turns out namespacing was not hard at all for variables.. 
just ran a test...

ADD1 = {}
ADD1.selectors, ADD1.countselectors = selectors()

but does not work on functions

function ADD1.something()

end
Details
Message ID
<5b95e410-ac12-d713-cb04-71c3b811c9bc@aoirthoir.com>
In-Reply-To
<55114164-7f2b-84b9-d27d-e221e610abc4@aoirthoir.com> (view parent)
DKIM signature
pass
Download raw message
This works...

ADD1 = {}

     function ADD1.selectors()
       selectors = config["selectors"]
       if not selectors then
         Plugin.exit("No selectors chosen")
       else
         selectors = Regex.split(selectors,",")
       end
       countselectors = size(selectors)
       return selectors, countselectors
     end

     ADD1.selectors, ADD1.countselectors = ADD1.selectors()

     return ADD1

if I put the function in a separate file it does not work...when i do a 
dofile() on that function...basically just means i include all the 
functions in one file. not a big deal but now it works.. i will be 
working tomorrow on them and then post for yall to review.
Daniil Baturin
Details
Message ID
<a18236bb-fd2e-f159-ed50-ed9f874c6b00@yandex.ru>
In-Reply-To
<55114164-7f2b-84b9-d27d-e221e610abc4@aoirthoir.com> (view parent)
DKIM signature
pass
Download raw message
This is a good stop-gap measure... I really need to get to making it
possible to pass lists and other non-string types to plugins though!

I'm starting to think I can do it even before the new TOML library is in
place, but I haven't proven that claim yet.
Details
Message ID
<0b23398d-98c6-b018-ecf2-333b26c7113e@baturin.org>
In-Reply-To
<55114164-7f2b-84b9-d27d-e221e610abc4@aoirthoir.com> (view parent)
DKIM signature
pass
Download raw message
Ok, now it's possible to pass lists of strings to plugins natively.

So plugin writers will be able to use either your comma-separated or
"real" lists since the next release.
Details
Message ID
<9ccd1c7d-24e8-167a-6bd2-bd130dcab5fd@aoirthoir.com>
In-Reply-To
<55114164-7f2b-84b9-d27d-e221e610abc4@aoirthoir.com> (view parent)
DKIM signature
pass
Download raw message
Very exciting! Yer awesome.

I have been programming now for about two weeks into the Soupault 
ecosystem and it took me one of those weeks to write the documentation. 
I don't have a lot but i just sat down and in about ten minutes 
refactored some of my code. Each time I do a refactor it is quick.

Yesterday i created two plugins, add-class.lua and add-style.lua both in 
just a few minutes together.

The loop for looping through all selectors is part of the code now so I 
can literally just do something like this in a plugin:


Plugin.require_version("1.8")
dofile("plugins/add1tocobol/add1tocobol.lua")
if not config.class then
   Plugin.exit("add-class.lua: No class chosen")
end
code = [[
   class = HTML.get_attribute(element,"class")
   if class then
     class = class.." "..config.class
   else
     class = config.class
   end
   HTML.set_attribute(element,"class",class)
]]
ADD1.loop_selectors(code)

or something simpler to color all elements:

Plugin.require_version("1.8")
dofile("plugins/add1tocobol/add1tocobol.lua")
code = [[
   ADD1.apply_random_color(element,"color",config.rejected)
]]
ADD1.loop_selectors(code)

I've described all of this at:

http://add1tocobol.com/add1tocobol-lua/
Export thread (mbox)