htmlbrowse/browse.rb

130 lines
2.2 KiB
Ruby
Raw Permalink Normal View History

2018-04-14 11:36:04 -05:00
require "nokogiri"
2019-06-23 10:40:43 -05:00
require "colorize"
require "css_parser"
include CssParser
2018-04-14 11:36:04 -05:00
$mode=""
pages={}
pages["/"]=<<-ENDPAGE
<html>
<p>Welcome to my website!</p>
<p><b>Bold</b></p>
2019-06-23 10:40:43 -05:00
<p><i>Italics</b></p>
<p>
2018-04-14 11:36:04 -05:00
<a href="/page">A page</a>
2019-06-23 10:40:43 -05:00
<br>
2018-04-14 11:36:04 -05:00
<a href="/anotherpage">Another page</a>
2019-06-23 10:40:43 -05:00
</p>
2018-04-14 11:36:04 -05:00
</html>
ENDPAGE
pages["/page"]=<<-ENDPAGE
<html>
<p>A page</p>
2019-06-23 10:40:43 -05:00
<p><b><i>Bold and Italic</i>hi</b></p>
<p>
2018-04-14 11:36:04 -05:00
<a href="/">Home</a>
2019-06-23 10:40:43 -05:00
<pr>
2018-04-14 11:36:04 -05:00
<a href="/anotherpage">Another page</a>
2019-06-23 10:40:43 -05:00
</p>
2018-04-14 11:36:04 -05:00
</html>
ENDPAGE
pages["/anotherpage"]=<<-ENDPAGE
<html>
<p>Here is more info</p>
2019-06-23 10:40:43 -05:00
<p>
2018-04-14 11:36:04 -05:00
<a href="/page">A page</a>
2019-06-23 10:40:43 -05:00
<br>
2018-04-14 11:36:04 -05:00
<a href="/">Home</a>
2019-06-23 10:40:43 -05:00
</p>
2018-04-14 11:36:04 -05:00
</html>
ENDPAGE
currentpage="/"
2019-06-23 10:40:43 -05:00
color_codes={
:black => 0, :light_black => 30,
:red => 1, :light_red => 61,
:green => 2, :light_green => 62,
:yellow => 3, :light_yellow => 63,
:blue => 4, :light_blue => 64,
:magenta => 5, :light_magenta => 65,
:cyan => 6, :light_cyan => 66,
:white => 7, :light_white => 67,
:default => 9
}
def showtext(text)
2018-04-14 11:36:04 -05:00
case $mode
when "b"
2019-06-23 10:40:43 -05:00
print "\e[1m"
2018-04-14 11:36:04 -05:00
when "i"
2019-06-23 10:40:43 -05:00
print "\e[3m"
2018-04-14 11:36:04 -05:00
when "ib"
2019-06-23 10:40:43 -05:00
print "\e[1m"
print "\e[3m"
2018-04-14 11:36:04 -05:00
end
2019-06-23 10:40:43 -05:00
print text
print "\e[0m"
2018-04-14 11:36:04 -05:00
end
def dispelem(elem)
if elem.class==Nokogiri::XML::Text
2019-06-23 10:40:43 -05:00
showtext(elem.to_s)
2018-04-14 11:36:04 -05:00
return
end
case elem.name
when "p"
elem.children.each do |elem|
dispelem(elem)
end
2019-06-23 10:40:43 -05:00
puts "\n"
when "br"
puts "\n"
2018-04-14 11:36:04 -05:00
when "a"
$links.push(elem["href"])
2019-06-23 10:40:43 -05:00
showtext(elem.children[0])
print "[#{$linkid}]"
2018-04-14 11:36:04 -05:00
$linkid+=1
when "b"
if $mode=="i"
$mode="ib"
else
$mode="b"
end
elem.children.each do |elem|
dispelem(elem)
end
2019-06-23 10:40:43 -05:00
if $mode=="ib"
$mode="i"
else
$mode=""
end
2018-04-14 11:36:04 -05:00
when "i"
if $mode=="b"
$mode="ib"
else
$mode="i"
end
elem.children.each do |elem|
dispelem(elem)
end
2019-06-23 10:40:43 -05:00
if $mode=="ib"
$mode="b"
else
$mode=""
end
2018-04-14 11:36:04 -05:00
end
end
while true
$links=[]
$linkid=1
system("clear")
doctext=pages[currentpage].gsub("\n","")
doc=Nokogiri::Slop(doctext)
doc.html.body.children.each do |elem|
dispelem(elem)
end
print "Link ID/exit:"
id=gets.chomp!.downcase
break if id=="exit"
currentpage=$links[id.to_i-1]
end