aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/core/browser.cc104
-rw-r--r--src/parser/html_lexer.cc6
-rw-r--r--src/parser/tree_builder.cc8
-rw-r--r--src/render/renderer.cc69
4 files changed, 153 insertions, 34 deletions
diff --git a/src/core/browser.cc b/src/core/browser.cc
index 4225986..8bd921c 100644
--- a/src/core/browser.cc
+++ b/src/core/browser.cc
@@ -23,17 +23,24 @@
#include "ricochet/render/renderer.hh"
#include "ricochet/tui/terminal.hh"
#include <cstddef>
+#include <exception>
#include <iostream>
namespace ricochet::core {
+struct PageData
+{
+ std::vector<std::string> lines;
+ std::vector<std::string> links;
+};
+
namespace {
-std::vector<std::string> load_page( std::string_view url )
+PageData load_page( std::string_view url )
{
const net::HttpClient client;
auto response_result = client.fetch( url );
if ( !response_result.has_value() ) {
- return { "[!] Failed to load: " + std::string( url ) };
+ return { .lines = { "[!] Failed to load: " + std::string( url ) }, .links = {} };
}
const parser::HtmlLexer lexer;
@@ -41,20 +48,20 @@ std::vector<std::string> load_page( std::string_view url )
const render::Renderer renderer;
const auto dom_root = builder.build( lexer.tokenize( response_result.value().body ) );
- const std::string text = renderer.render( dom_root );
+ const render::RenderResult result = renderer.render( dom_root );
std::vector<std::string> lines;
std::size_t start = 0;
- while ( start < text.length() ) {
- const std::size_t end = text.find( '\n', start );
+ while ( start < result.text.length() ) {
+ const std::size_t end = result.text.find( '\n', start );
if ( end == std::string::npos ) {
- lines.push_back( text.substr( start ) );
+ lines.push_back( result.text.substr( start ) );
break;
}
- lines.push_back( text.substr( start, end - start ) );
+ lines.push_back( result.text.substr( start, end - start ) );
start = end + 1;
}
- return lines;
+ return { .lines = lines, .links = result.links };
}
void draw_view( const tui::Terminal& terminal,
@@ -73,7 +80,7 @@ void draw_view( const tui::Terminal& terminal,
std::cout << "~\r\n";
}
}
- std::cout << "\033[7m URL: " << url << " | [j/k] Scroll [/] Jump [q] Quit \033[0m";
+ std::cout << "\033[7m URL: " << url << " | [j/k] Scroll [/] Jump [f] Follow [q] Quit \033[0m";
std::cout.flush();
}
@@ -115,6 +122,79 @@ std::string get_url_input( const tui::Terminal& terminal )
return input;
}
+std::string get_link_input( const tui::Terminal& terminal )
+{
+ auto [width, height] = terminal.get_size();
+ terminal.move_cursor( height, 1 );
+ std::cout << "\x1b[2K\033[7m Follow Link [#]: \033[0m ";
+ std::cout.flush();
+
+ std::string num_input;
+ terminal.show_cursor( true );
+ while ( true ) {
+ const char ch = terminal.read_key();
+ if ( ch == '\r' || ch == '\n' ) {
+ break;
+ }
+ if ( ch == 27 ) {
+ num_input.clear();
+ break;
+ }
+ if ( ch == 127 || ch == 8 ) {
+ if ( !num_input.empty() ) {
+ num_input.pop_back();
+ std::cout << "\b \b";
+ std::cout.flush();
+ }
+ } else if ( std::isdigit( static_cast<unsigned char>( ch ) ) ) {
+ num_input += ch;
+ std::cout << ch;
+ std::cout.flush();
+ }
+ }
+ terminal.show_cursor( false );
+ return num_input;
+}
+
+void handle_follow_link( const tui::Terminal& terminal,
+ const std::vector<std::string>& links,
+ std::string& current_url,
+ bool& navigate )
+{
+ const std::string num_input = get_link_input( terminal );
+ if ( !num_input.empty() ) {
+ try {
+ const std::size_t index = std::stoul( num_input );
+ if ( index > 0 && index <= links.size() ) {
+ std::string target = links[index - 1];
+
+ if ( target.starts_with( "/" ) ) {
+ if ( target.starts_with( "//" ) ) {
+ target = "https:" + target; // "//example.com"
+ } else {
+ const std::size_t slash_pos = current_url.find( '/', 8 ); // 跳过 https://
+ const std::string domain
+ = ( slash_pos == std::string::npos ) ? current_url : current_url.substr( 0, slash_pos );
+ target = domain + target; // "/about" -> "https://gnu.org/about"
+ }
+ } else if ( !target.starts_with( "http" ) ) {
+ const std::size_t last_slash = current_url.rfind( '/' );
+ if ( last_slash > 7 ) {
+ target = current_url.substr( 0, last_slash + 1 ) + target;
+ } else {
+ target = current_url + "/" + target;
+ }
+ }
+
+ current_url = target;
+ navigate = true;
+ }
+ } catch ( const std::exception& e ) {
+ (void)e; // NOLINT
+ }
+ }
+}
+
} // namespace
int Browser::run( std::string_view initial_url )
@@ -126,7 +206,9 @@ int Browser::run( std::string_view initial_url )
terminal.clear_screen();
while ( !current_url.empty() ) {
- auto lines = load_page( current_url );
+ auto page_data = load_page( current_url );
+ const auto& lines = page_data.lines;
+ const auto& links = page_data.links;
std::size_t scroll_y = 0;
bool navigate = false;
@@ -151,6 +233,8 @@ int Browser::run( std::string_view initial_url )
current_url = std::move( next_url );
navigate = true;
}
+ } else if ( c == 'f' ) {
+ handle_follow_link( terminal, links, current_url, navigate );
}
}
}
diff --git a/src/parser/html_lexer.cc b/src/parser/html_lexer.cc
index 73591b3..2aa05b1 100644
--- a/src/parser/html_lexer.cc
+++ b/src/parser/html_lexer.cc
@@ -90,12 +90,8 @@ std::vector<HtmlToken> HtmlLexer::tokenize( // NOLINT(readability-convert-member
tokens.emplace_back( TagOpenToken { buffer } );
buffer.clear();
state = LexerState::Data;
- } else if ( std::isspace( static_cast<unsigned char>( c ) ) ) {
- tokens.emplace_back( TagOpenToken { buffer } );
- buffer.clear();
- state = LexerState::SwallowAttributes;
} else {
- buffer += static_cast<char>( std::tolower( static_cast<unsigned char>( c ) ) );
+ buffer += c;
}
break;
case LexerState::ClosingTagName:
diff --git a/src/parser/tree_builder.cc b/src/parser/tree_builder.cc
index 4f7f87e..64f841b 100644
--- a/src/parser/tree_builder.cc
+++ b/src/parser/tree_builder.cc
@@ -36,10 +36,14 @@ struct TokenVisitor
void operator()( const TagOpenToken& token ) const
{
+ const std::size_t space_pos = token.name.find( ' ' );
+ const std::string base_name
+ = ( space_pos == std::string::npos ) ? token.name : token.name.substr( 0, space_pos );
+
DomNode new_node { .tag_name = token.name, .text_content = "", .children = {} };
- if ( token.name == "meta" || token.name == "link" || token.name == "img" || token.name == "br"
- || token.name == "hr" || token.name == "input" || token.name == "base" || token.name == "col" ) {
+ if ( base_name == "meta" || base_name == "link" || base_name == "img" || base_name == "br" || base_name == "hr"
+ || base_name == "input" || base_name == "base" || base_name == "col" ) {
stack->back().children.push_back( std::move( new_node ) );
} else {
diff --git a/src/render/renderer.cc b/src/render/renderer.cc
index 2115ee1..1af40f1 100644
--- a/src/render/renderer.cc
+++ b/src/render/renderer.cc
@@ -17,7 +17,7 @@
*/
#include "ricochet/render/renderer.hh"
-#include <iostream>
+#include <array>
namespace ricochet::render {
@@ -42,51 +42,86 @@ std::string decode_entities( std::string text ) // NOLINT(readability-convert-me
return text;
}
-void render_node( const parser::DomNode& node, std::string& output ) // NOLINT(misc-no-recursion)
+std::string extract_href( const std::string& tag_name )
{
- if ( node.tag_name == "head" || node.tag_name == "style" || node.tag_name == "script" || node.tag_name == "meta"
- || node.tag_name == "title" || node.tag_name == "link" ) {
+ std::size_t pos = tag_name.find( "href=\"" );
+ if ( pos != std::string::npos ) {
+ pos += 6;
+ const std::size_t end = tag_name.find( '\"', pos );
+ if ( end != std::string::npos ) {
+ return tag_name.substr( pos, end - pos );
+ }
+ }
+ pos = tag_name.find( "href='" );
+ if ( pos != std::string::npos ) {
+ pos += 6;
+ const std::size_t end = tag_name.find( '\'', pos );
+ if ( end != std::string::npos ) {
+ return tag_name.substr( pos, end - pos );
+ }
+ }
+ return "";
+}
+
+void render_node( const parser::DomNode& node, // NOLINT(misc-no-recursion)
+ std::string& output,
+ std::vector<std::string>& links )
+{
+ const std::size_t space_pos = node.tag_name.find( ' ' );
+ const std::string base_tag
+ = ( space_pos == std::string::npos ) ? node.tag_name : node.tag_name.substr( 0, space_pos );
+
+ if ( base_tag == "head" || base_tag == "style" || base_tag == "script" || base_tag == "meta"
+ || base_tag == "title" || base_tag == "link" ) {
return;
}
- if ( node.tag_name.empty() ) {
+ if ( base_tag.empty() ) {
output += decode_entities( node.text_content );
return;
}
- const bool is_header
- = ( node.tag_name == "h1" || node.tag_name == "h2" || node.tag_name == "h3" || node.tag_name == "h4" );
+ const bool is_header = ( base_tag == "h1" || base_tag == "h2" || base_tag == "h3" || base_tag == "h4" );
+ const bool is_link = ( base_tag == "a" );
if ( is_header ) {
output += "\n\n\033[1;31m";
- } else if ( node.tag_name == "a" ) {
- std::cout << "\033[4;34m";
- } else if ( node.tag_name == "p" || node.tag_name == "div" || is_header ) {
+ } else if ( is_link ) {
+ output += "\033[4;34m"; // 【核心修复】:必须是 output += ,绝对不能用 std::cout
+ } else if ( base_tag == "p" || base_tag == "div" || is_header ) {
if ( !output.empty() && output.back() != '\n' ) {
output += "\n";
}
}
for ( const auto& child : node.children ) {
- render_node( child, output );
+ render_node( child, output, links );
}
- if ( is_header || node.tag_name == "a" ) {
+
+ if ( is_header ) {
+ output += "\033[0m";
+ } else if ( is_link ) {
output += "\033[0m";
+ const std::string href = extract_href( node.tag_name );
+ if ( !href.empty() && !href.starts_with( "javascript:" ) && !href.starts_with( "#" ) ) {
+ links.push_back( href );
+ output += " \033[7m[" + std::to_string( links.size() ) + "]\033[0m"; // 打印数字标签!
+ }
}
- if ( node.tag_name == "h1" || node.tag_name == "p" || node.tag_name == "div" ) {
+ if ( base_tag == "h1" || base_tag == "p" || base_tag == "div" ) {
output += "\n";
}
}
} // namespace
-std::string Renderer::render( // NOLINT(readability-convert-member-functions-to-static)
+RenderResult Renderer::render( // NOLINT(readability-convert-member-functions-to-static)
const parser::DomNode& node ) const
{
- std::string output;
- render_node( node, output );
- return output;
+ RenderResult result;
+ render_node( node, result.text, result.links );
+ return result;
}
} // namespace ricochet::render \ No newline at end of file