luajitos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

moonbrowser.lua (3059B)


      1 -- Moonbrowser - HTML Renderer for LuajitOS
      2 -- Renders HTML files to screen or window
      3 
      4 local render = require("render")
      5 
      6 -- Parse arguments
      7 local htmlPath = args.o or args.open or args[1]
      8 
      9 print("Moonbrowser: args.o = " .. tostring(args.o))
     10 print("Moonbrowser: args.open = " .. tostring(args.open))
     11 print("Moonbrowser: args[1] = " .. tostring(args[1]))
     12 
     13 if not htmlPath or htmlPath == "" then
     14     print("Moonbrowser: No HTML file path provided")
     15     print("Usage: moonbrowser -o <path_to_file.html>")
     16     print("       moonbrowser --open <path_to_file.html>")
     17     print("       moonbrowser <path_to_file.html>")
     18     return
     19 end
     20 
     21 print("Moonbrowser: Opening " .. htmlPath)
     22 
     23 -- Trim whitespace from path
     24 htmlPath = htmlPath:match("^%s*(.-)%s*$")
     25 
     26 -- Expand ~ to home directory if needed
     27 if htmlPath:sub(1, 1) == "~" then
     28     htmlPath = htmlPath:gsub("^~", "/home")
     29 end
     30 
     31 print("Moonbrowser: Loading HTML from: " .. htmlPath)
     32 
     33 -- Check if SafeFS is available
     34 if not fs then
     35     print("Moonbrowser: ERROR - SafeFS not available (missing 'filesystem' permission)")
     36     return
     37 end
     38 
     39 -- Check if file exists
     40 if not fs:exists(htmlPath) then
     41     print("Moonbrowser: ERROR - File does not exist: " .. htmlPath)
     42     return
     43 end
     44 
     45 -- Read the HTML file using SafeFS
     46 local html_content, err = fs:read(htmlPath)
     47 if not html_content then
     48     print("Moonbrowser: ERROR - Failed to read file: " .. tostring(err))
     49     return
     50 end
     51 
     52 print("Moonbrowser: Read " .. #html_content .. " bytes")
     53 
     54 -- Parse and prepare the document
     55 local doc, parse_err
     56 local success, err_msg = pcall(function()
     57     doc, parse_err = render.openAll(htmlPath)
     58 end)
     59 
     60 if not success then
     61     print("Moonbrowser: ERROR - Exception during parse: " .. tostring(err_msg))
     62     return
     63 end
     64 
     65 if not doc then
     66     print("Moonbrowser: ERROR - Failed to parse HTML: " .. tostring(parse_err or "unknown error"))
     67     return
     68 end
     69 
     70 print("Moonbrowser: HTML parsed successfully")
     71 
     72 -- Create a window for rendering
     73 local window = app:newWindow("Moonbrowser", 800, 600, true)
     74 
     75 -- State variables
     76 local scrollY = 0
     77 local pageHeight = 1200
     78 
     79 -- Set up drawing callback
     80 window.onDraw = function(gfx)
     81     -- Clear background to white
     82     gfx:fillRect(0, 0, window.width, window.height, 0xFFFFFF)
     83 
     84     -- Render the HTML document
     85     local render_ok, render_err = pcall(function()
     86         doc:render(gfx, window.width, pageHeight)
     87     end)
     88 
     89     if not render_ok then
     90         gfx:drawText(10, 10, "Render error: " .. tostring(render_err), 0xFF0000)
     91     end
     92 
     93     -- Draw status bar at bottom
     94     gfx:fillRect(0, window.height - 20, window.width, 20, 0x333333)
     95     gfx:drawText(5, window.height - 16, "Moonbrowser - " .. htmlPath, 0xFFFFFF)
     96 end
     97 
     98 -- Handle input
     99 window.onInput = function(key, scancode)
    100     if key == "q" or key == "Q" then
    101         window:close()
    102     elseif scancode == 72 then  -- Up arrow
    103         scrollY = math.max(0, scrollY - 50)
    104         window:markDirty()
    105     elseif scancode == 80 then  -- Down arrow
    106         scrollY = scrollY + 50
    107         window:markDirty()
    108     end
    109 end
    110 
    111 print("Moonbrowser: Window created and ready")