Omnimaga

Calculator Community => TI Calculators => Lua => Topic started by: Nick on December 18, 2011, 03:48:21 am

Title: Lua 'XML' reader
Post by: Nick 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...

(http://img.removedfromgame.com/imgs/xmlreaderscreen.GIF)

you can find the source here: https://github.com/NickSteen/Lua-XML-reader/
thanks to adriweb and excale :)
Title: Re: Lua 'XML' reader
Post by: Adriweb 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 :)
Title: Re: Lua 'XML' reader
Post by: Nick 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
Title: Re: Lua 'XML' reader
Post by: Adriweb 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...
Title: Re: Lua 'XML' reader
Post by: Nick 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
Title: Re: Lua 'XML' reader
Post by: ExtendeD 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 (https://gist.github.com/).
Title: Re: Lua 'XML' reader
Post by: Adriweb 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/ (https://github.com/NickSteen/Lua-XML-reader/)
Title: Re: Lua 'XML' reader
Post by: Deep Toaster 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.
Title: Re: Lua 'XML' reader
Post by: Adriweb 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 :

(http://i.imgur.com/AT5UB.jpg[)

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"}
}
Title: Re: Lua 'XML' reader
Post by: flyingfisch 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?
Title: Re: Lua 'XML' reader
Post by: Adriweb on December 18, 2011, 03:28:55 pm
Good for me ;)
Title: Re: Lua 'XML' reader
Post by: Jim Bauwens on December 18, 2011, 03:33:20 pm
Interesting :)
I'm sure these stuff will be put to good use.
Title: Re: Lua 'XML' reader
Post by: flyingfisch on December 18, 2011, 04:04:43 pm
Good for me ;)

Thx :)
Title: Re: Lua 'XML' reader
Post by: Nick 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 :)
Title: Re: Lua 'XML' reader
Post by: Adriweb 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 (https://github.com/adriweb/EEPro-for-Nspire) ?
We need skilled coders like you ;)
Title: Re: Lua 'XML' reader
Post by: renatose on December 18, 2011, 05:05:33 pm
how hard would it be to create a pdf reader for nspire in C?

just wondering...
Title: Re: Lua 'XML' reader
Post by: Adriweb on December 18, 2011, 05:06:09 pm
pdf [readers] are extremely hard to make...
Title: Re: Lua 'XML' reader
Post by: renatose on December 18, 2011, 05:11:36 pm
hmm... and what about an odt or doc reader? (reader only - not editor)
Title: Re: Lua 'XML' reader
Post by: blauemauritius on April 15, 2013, 12:55:21 pm
Hello,

the code doesn't work on my TI-nspire CX CAS. I have copied the code in a txt-editor and saved the file with the name xml.lua. With TI-Nspire Scripting I have tried to insert it on my TI. The page is white only. What is wrong? Maybe someone could help me.

Thank you very much.
Title: Re: Lua 'XML' reader
Post by: Nick on April 17, 2013, 12:29:30 pm
Try adding this line at the top of the script (very first line of file)

Code: [Select]
platform.apilevel = '1.0'

You're probably unning OS 3.2, and from then a newer API is used (API 2.0), so you have to tell the calc it needs API 1.0