Author Topic: Lua 'XML' reader  (Read 11447 times)

0 Members and 1 Guest are viewing this topic.

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Lua 'XML' reader
« on: December 18, 2011, 03:48:21 am »
in that topic about an  e-book reader for the nspire, they came up with the idea to read from xml files within lua.
I thought of it, and designed somehow a construction that is similar to xml (tags) but readable for lua.

this is the format:
Code: [Select]
xml = {
"<ttl>Hello",
"<txt>this is a test to show you the possibilities of xml in Nspire lua",
"<txt>It's very simple, just type a tag like <ttl> and than your title",
"<img>\030\000\000\000\030\000\000\037\/*find yourself a pic to insert, it would be too long to post */137\194\137\194\137\198\207\210x\235\000\000\000\000",
"<txt>and it automatically scrolls down to the next line after the image",
"<txt>this is the last sentence!"
}
as you can see, it's not xml at all, but you can give text a separate font by just using my script.
it's very simple, but untill now only <ttl> (title), <txt> (normal text) and <img> (image) are implemented.

Please give comments/suggestions to make this better, it could be very useful!

of course it ain't possible to read from real XML, so it's still not that usable for that e-book reader, since you have to write/let a program write it to the lue xml format..
as you can see no endtags are used,it is just not required...



you can find the source here: https://github.com/NickSteen/Lua-XML-reader/
thanks to adriweb and excale :)
« Last Edit: December 18, 2011, 05:51:48 am by Nick »

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua 'XML' reader
« Reply #1 on: December 18, 2011, 04:23:56 am »
Oh, nice !
:D

( Y U NO endtags ? )

You have so many projects you should put them all (their source) on GitHub, it's really good, there :)
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Lua 'XML' reader
« Reply #2 on: December 18, 2011, 04:42:19 am »
because i (well, not i, just the calc xp) read the line, and seperate the tags, i can easely add them if you want to, it's 2 seconds of work, i just thought it would be easier to not use them as it work per line (the tag ends at the end of the string).

and i'm having a look at that github, it would be a good idea to post it there, for having the most recent version every time

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua 'XML' reader
« Reply #3 on: December 18, 2011, 04:45:27 am »
because i (well, not i, just the calc xp) read the line, and seperate the tags, i can easely add them if you want to, it's 2 seconds of work, i just thought it would be easier to not use them as it work per line (the tag ends at the end of the string).
Oh, it's not a bad idea you had, I was just asking out of curiosity ! :)

and i'm having a look at that github, it would be a good idea to post it there, for having the most recent version every time
Exactly  !
I recommand using SmartGit (on Windows) and GitHub for Mac (well, on Mac :P).
Once you know how to use Git/Github, it's really awesome :)
And it's like an onlince backup of your stuff... this is a lifesaver sometimes...
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Lua 'XML' reader
« Reply #4 on: December 18, 2011, 05:06:40 am »
i think i'll just upload them to my own site and refer to that page, since i don't manage to get the github working xs i followed the tutorial to install it, and i did everything required, but it does not work xs

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: Lua 'XML' reader
« Reply #5 on: December 18, 2011, 05:36:31 am »
Nick, you coud also directly use Lua structures instead of specific markup, this maybe more simple to parse:
Code: [Select]
markup = {
 {ttl = "Hello"},
 "this is a test to show you the possibilities of xml in Nspire lua",
 "It's very simple, just type a tag like <ttl> and than your title",
 {img = "\030\000\000\000\030\000\000\037\/*find yourself a pic to insert, it would be too long to post */137\194\137\194\137\198\207\210x\235\000\000\000\000"},
 "and it automatically scrolls down to the next line after the image",
 "this is the last sentence!"
}

for i, v in ipairs(markup) do
local t = type(v)
if t == "table" then
if v.ttl then print('Title: ', v.ttl)
elseif v.img then print('Image: ', v.img)
end
else
print(v)
end
end

i think i'll just upload them to my own site and refer to that page, since i don't manage to get the github working xs i followed the tutorial to install it, and i did everything required, but it does not work xs

You can also the easy to use Gist.
Ndless.me with the finest TI-Nspire programs

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua 'XML' reader
« Reply #6 on: December 18, 2011, 05:47:57 am »
Good idea, Extended, though the xml syntax itself tends to disappear now... :P

Anyway, Nick succeded to set up his GitHub account !

Here's this project :

https://github.com/NickSteen/Lua-XML-reader/
« Last Edit: December 18, 2011, 05:48:27 am by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Deep Toaster

  • So much to do, so much time, so little motivation
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 8217
  • Rating: +758/-15
    • View Profile
    • ClrHome
Re: Lua 'XML' reader
« Reply #7 on: December 18, 2011, 01:47:52 pm »
Could this become an XHTML parser someday? :D

But it seems like using Lua structures (like ExtendeD suggested) would be even more useful.




Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua 'XML' reader
« Reply #8 on: December 18, 2011, 03:03:55 pm »
here's an even more improved version :

Code: [Select]
-- Adriweb
-- TI-Planet

-- Original Idea by Nick Steen, Some things added by Extended

platform.apilevel = '1.0'

count = 0

markup = {
 {ttl = "hello"},
 {txt = "hallo"},
 {center = {txt = "centered text"} },
 {txt = "goodbye"},
 {bold = {txt = "oh wait ! bold text !!"} },
 {img = "\016\000\000\000\018\000\000\000\000\000\000\000 \000\000\000\016\000\001\000\156\243\156\243\156\243\189\247\213\222\235\193\136\181g\181\135\181g\181\137\185O\202\189\247\156\243\156\243\156\243\156\243\156\243{\239O\206\168\222F\243D\243d\247d\247#\243\004\235E\214\170\1858\231\156\243\156\243\156\243\213\222m\210\231\230d\247\132\251\195\255\131\255\131\255\131\255C\251\003\247\132\226\233\193r\210z\239\156\243\204\189\198\226\132\247\166\218\007\198\198\226\195\255\131\255#\243\231\197\007\198\002\251\131\230\135\185\246\222\212\218g\210D\243\166\218\166\218\197\226D\239\131\255\131\255C\247\165\218d\222\006\206\194\242\196\205O\206\170\185\005\239\131\255\197\226D\239\131\255\131\255\131\255\131\255C\251\034\255\002\255E\218\226\250b\234\167\193G\173C\247\131\255\131\255\130\218\001\198\162\222\131\255c\251\002\243\161\197\161\197\226\250\226\250\162\246\133\193f\181#\243C\251\131\255\194\193\194\156\002\202C\251\034\255\162\234\194\156\194\156\193\250\193\254\193\254\133\193g\181\003\247c\251\034\255\196\230e\218\196\234\034\255\034\255\226\242%\218%\218\226\250\226\250\161\254\133\193&\169\226\242\034\255\034\255\034\255\034\255\034\255\002\255\002\255\193\254\193\254\226\250\161\254\161\254\161\254d\189G\173\163\234\002\255\226\242\194\242\163\234\162\242\162\242\162\242\130\238\130\242B\242\130\242\162\246B\242\133\193\014\198E\214\194\242f\181\198\156\231\156\231\156\231\156\231\156\231\156\231\156\198\156\231\156B\242\227\213\235\197Y\235\136\185\130\238\193\254\132\189s\206\222\251\222\251\222\251\222\251{\239\198\156\130\242\129\254d\189\179\214\156\243\237\193\196\205\162\242\162\242\198\201)\165)\165)\165)\165H\173B\242\129\254\227\213\169\193\023\227\156\243z\239\137\185\034\226\162\246\130\242B\234\034\230\034\230B\234\034\234\129\254\130\242D\181\213\222\156\243\156\243\156\243\023\227\201\197\002\222\162\246\161\254\129\254\161\254\129\254\129\250\034\234d\189\147\214\156\243\156\243\156\243\156\243\156\243\246\222\011\206\196\205\002\222B\238\034\238\034\230\196\209\165\197\147\214{\239\156\243\156\243\156\243\189\247\156\243\156\243\023\227p\210\011\206\198\205\198\205\232\205O\210\147\214\156\243\156\243\156\243\156\243"},
 {txt = "really, goodbye"}
}

function on.paint(gc)
   if go then
       go = false
       print("----")
       Parse(gc, markup, {centered=false})
       count = 0
   end
end

function Parse(gc, data, options)
    for i, v in ipairs(data) do
        local t = type(v)
        if t == "table" then
            if v.ttl then count = count + 1 gc:setFont("serif","b",18) myPrint(gc, v.ttl, count-1, {})
            elseif v.txt then gc:setFont("serif","r",12) myPrint(gc, v.txt, count, options)
            elseif v.img then local tmpImg = image.new(v.img) gc:drawImage(tmpImg,0,image.height(tmpImg)*(count-2)) print('Image displayed')
            elseif v.center then Parse(gc, {v.center}, {centered=true})
            elseif v.bold then Parse(gc, {v.bold}, {bold=true})
            end
            count = count + 1
        else
            print("what's going on here ?")
            myPrint(gc, v, count, {centered=false})
        end
    end
end

function drawXCenteredString(gc,str,y)
    gc:drawString(str, (pww() - gc:getStringWidth(str)) / 2, y, "top")
end

function myPrint(gc, string, y, options)
    if options.centered then
        drawXCenteredString(gc,string,14*y)
    elseif options.bold then
        gc:setFont("serif","b",12)       
        gc:drawString(string,1,14*y,"top")
    else
        gc:drawString(string,1,14*y,"top")
    end
    print(string)
end
 
function pww()
    return platform.window:width()
end

function pwh()
    return platform.window:height()
end

function on.enterKey()
    go = true
end



It does that :



from :

Code: [Select]
markup = {
 {ttl = "hello"},
 {txt = "hallo"},
 {center = {txt = "centered text"} },
 {txt = "goodbye"},
 {bold = {txt = "oh wait ! bold text !!"} },
 {img = //image-string//},
 {txt = "really, goodbye"}
}
« Last Edit: December 18, 2011, 03:10:51 pm by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: Lua 'XML' reader
« Reply #9 on: December 18, 2011, 03:28:32 pm »
Hey, I was just thinking of making a similar prog for casio calcs. Do you mind if I use some of your code and make an attribution somewhere?
« Last Edit: December 18, 2011, 03:28:46 pm by flyingfisch »



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua 'XML' reader
« Reply #10 on: December 18, 2011, 03:28:55 pm »
Good for me ;)
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua 'XML' reader
« Reply #11 on: December 18, 2011, 03:33:20 pm »
Interesting :)
I'm sure these stuff will be put to good use.

Offline flyingfisch

  • I'm 1337 now!
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1620
  • Rating: +94/-17
  • Testing, testing, 1...2...3...4...5...6...7...8..9
    • View Profile
    • Top Page Website Design
Re: Lua 'XML' reader
« Reply #12 on: December 18, 2011, 04:04:43 pm »



Quote from: my dad
"welcome to the world of computers, where everything seems to be based on random number generators"



The Game V. 2.0

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Lua 'XML' reader
« Reply #13 on: December 18, 2011, 04:40:01 pm »
hmm, adriweb, i think you should pick up this project instead of me, your coding skills are way better, and tbh, your code doesn't look like mine in any way, it's much better..
so take it if you want :)

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Lua 'XML' reader
« Reply #14 on: December 18, 2011, 04:46:24 pm »
Well, thanks for your comments, but I don't have time right now... :(

Maybe later :)

Also, would you like to participate in EEPro ?
We need skilled coders like you ;)
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation