gemtext.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. --#!/usr/bin/lua
  2. -- Gemline DEMO
  3. -- USE: <margin> gemtext files
  4. render = require( "gemline" )
  5. links = {}
  6. lines = {}
  7. pages = {}
  8. margin = 66
  9. function loadPages( len )
  10. len = len or margin
  11. pages[#pages+1] = 1
  12. local l = 0
  13. for line, rows in pairs( lines ) do
  14. if l + #rows > len then
  15. pages[#pages+1] = line
  16. l = #rows
  17. else
  18. if string.sub( rows[1], 1, 2 ) == "--" then --commento
  19. if string.sub( rows[1], 3, 3 ) == "-" and
  20. line < #lines
  21. then -- new page
  22. pages[#pages+1] = line + 1
  23. l = #rows
  24. end
  25. else
  26. l = l + #rows
  27. end
  28. end
  29. end
  30. end
  31. function printPages( first, last )
  32. first = first or 1
  33. if first > #pages then first = #pages end
  34. last = last or first
  35. if type( last ) == "boolean" then last = #pages end
  36. for page = first, last do
  37. if page == #pages then
  38. lastLine = #lines
  39. else
  40. lastLine = pages[page+1]-1
  41. end
  42. io.write( "\27[H\27[2J" ) -- ERASE SCREEN
  43. local l = 0
  44. for line = pages[page], lastLine do
  45. local rows = lines[line]
  46. if string.sub( rows[1], 1, 2 ) ~= "--" then --commento
  47. for n, row in pairs( rows ) do
  48. io.write( row )
  49. end
  50. end
  51. end
  52. io.write( "\nPage ", page, " of ", #pages, "\n" )
  53. end
  54. end
  55. if #arg == 0 then
  56. for line in io.lines( "gemline.gmi" ) do
  57. lines[#lines+1] = render( line, margin )
  58. end
  59. loadPages( margin )
  60. printPages( 1, true)
  61. print()
  62. else
  63. for a = 1, #arg do
  64. if tonumber( arg[a] ) then
  65. margin = tonumber( arg[a] )
  66. -- 1/1= 74, 2/3=51, 1/2=38 e 1/3=27
  67. if margin > 74 then margin = 74 end
  68. if margin < 27 then margin = 27 end
  69. print( "\nRight margin: "..margin )
  70. else
  71. lines = {}
  72. print()
  73. for line in io.lines( arg[a] ) do
  74. lines[#lines+1] = render( line, margin )
  75. end
  76. loadPages( margin )
  77. local page = 1
  78. while true do
  79. printPages( page )
  80. io.write("Return cicle pages or cmd q quit > ")
  81. cmd = io.read()
  82. if string.lower(cmd) == "q" then
  83. io.write("Bye!\n")
  84. break
  85. end
  86. if page == #pages then page = 1 else page = page + 1 end
  87. end
  88. end
  89. end
  90. end