aboutsummaryrefslogtreecommitdiff
path: root/src/render/renderer.cc
blob: 5f65ea787967e5c9a75f0b2d668c51b9349ded29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Ricochet - A minimalist CLI web browser for fast, distraction-free navigation.
 * Copyright (C) 2026  Richard Qin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "ricochet/render/renderer.hh"
#include <array>
#include <cstdint>
#include <stdexcept>

namespace ricochet::render {

namespace {

std::string decode_entities( std::string text )
{
  constexpr std::array<std::pair<std::string_view, std::string_view>, 25> entities
    = { { { "&nbsp;", " " },   { "&lt;", "<" },     { "&gt;", ">" },     { "&amp;", "&" },   { "&quot;", "\"" },
          { "&apos;", "'" },   { "&copy;", "©" },   { "&reg;", "®" },    { "&trade;", "™" }, { "&euro;", "€" },
          { "&pound;", "£" },  { "&yen;", "¥" },    { "&cent;", "¢" },   { "&sect;", "§" },  { "&para;", "¶" },
          { "&hellip;", "…" }, { "&dagger;", "†" }, { "&Dagger;", "‡" }, { "&bull;", "•" },  { "&prime;", "′" },
          { "&Prime;", "″" },  { "&lsaquo;", "‹" }, { "&rsaquo;", "›" }, { "&oline;", "‾" }, { "&frasl;", "⁄" } } };

  for ( const auto& [entity, replacement] : entities ) {
    std::size_t pos = 0;
    while ( ( pos = text.find( entity, pos ) ) != std::string::npos ) {
      text.replace( pos, entity.length(), replacement );
      pos += replacement.length();
    }
  }

  std::size_t pos = 0;
  while ( ( pos = text.find( "&#", pos ) ) != std::string::npos ) {
    const std::size_t end = text.find( ';', pos );
    if ( end != std::string::npos && end - pos < 10 ) {
      const std::string code = text.substr( pos + 2, end - pos - 2 );
      try {
        std::uint32_t cp = 0;
        if ( !code.empty() && ( code[0] == 'x' || code[0] == 'X' ) ) {
          cp = std::stoul( code.substr( 1 ), nullptr, 16 );
        } else {
          cp = std::stoul( code );
        }
        if ( cp < 128 ) {
          text.replace( pos, end - pos + 1, 1, static_cast<char>( cp ) );
          continue;
        }
      } catch ( const std::invalid_argument& ) {
        (void)0; // Not a valid number, leave as-is
      } catch ( const std::out_of_range& ) {
        (void)0; // Value too large, leave as-is
      }
    }
    pos++;
  }
  return text;
}

std::string extract_href( const std::string& tag_name )
{
  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 "";
}

bool is_ignored_tag( std::string_view tag )
{
  return tag == "head" || tag == "style" || tag == "script" || tag == "meta" || tag == "title" || tag == "link";
}

bool is_heading_tag( std::string_view tag )
{
  return tag == "h1" || tag == "h2" || tag == "h3" || tag == "h4";
}

void render_node( const parser::DomNode& node, // NOLINT(misc-no-recursion)
                  RenderResult& result )       // NOLINT(bugprone-easily-swappable-parameters)
{
  std::string& output = result.text;
  std::vector<std::string>& links = result.links;
  std::vector<std::string>& inputs = result.inputs;

  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 ( is_ignored_tag( base_tag ) ) {
    return;
  }

  if ( base_tag.empty() ) {
    output += decode_entities( node.text_content );
    return;
  }

  const bool is_header = is_heading_tag( base_tag );
  const bool is_link = ( base_tag == "a" );
  const bool is_list_item = ( base_tag == "li" );
  const bool is_input = ( base_tag == "input" || base_tag == "textarea" );

  if ( is_header ) {
    output += "\n\n\033[1;31m";
  } else if ( is_link ) {
    output += "\033[4;34m";
  } else if ( is_list_item ) {
    output += "\n  • ";
  } else if ( is_input ) {
    inputs.emplace_back( "input_field" );
    output += " \033[7;33m[I" + std::to_string( inputs.size() ) + "]\033[0m ";
  } 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, result );
  }

  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( decode_entities( href ) );
      output += " \033[7m[" + std::to_string( links.size() ) + "]\033[0m"; // 打印数字标签!
    }
  }

  if ( base_tag == "h1" || base_tag == "p" || base_tag == "div" ) {
    output += "\n";
  }

  if ( base_tag == "img" ) {
    std::size_t alt_pos = node.tag_name.find( "alt=\"" );
    if ( alt_pos != std::string::npos ) {
      alt_pos += 5;
      const std::size_t alt_end = node.tag_name.find( '"', alt_pos );
      output += " \033[90m[Image: " + node.tag_name.substr( alt_pos, alt_end - alt_pos ) + "]\033[0m ";
    } else {
      output += " \033[90m[IMAGE]\033[0m ";
    }
  }
}

} // namespace

RenderResult Renderer::render( // NOLINT(readability-convert-member-functions-to-static)
  const parser::DomNode& node ) const
{
  RenderResult result;
  render_node( node, result );
  return result;
}

} // namespace ricochet::render