foreseen

A text-based gemini browser with prefetching
git clone git://git.laack.co/foreseen.git
Log | Files | Refs | README | LICENSE

link.cpp (3535B)


      1 #include "../include/link.hpp"
      2 
      3 #include <cstdint>
      4 #include <optional>
      5 #include <string>
      6 
      7 #include "../include/shared.hpp"
      8 #include "../include/utils.hpp"
      9 #include "../vendor/uri.hpp"
     10 
     11 Link::Link(std::string text, std::optional<uri> prior, int linkNumber) {
     12     this->linkNumber = linkNumber;
     13 
     14     if (text.substr(0, 2) != "=>") {
     15         linkDestination = parseDestination(text, prior);
     16         return;
     17     }
     18 
     19     std::string afterPrefix = text;
     20 
     21     afterPrefix = text.substr(2, text.size());
     22 
     23     int highest = -1;
     24 
     25     for (int i = 0; i < (int)afterPrefix.size(); ++i) {
     26         if (!(isWhiteSpace(afterPrefix, i))) {
     27             break;
     28         }
     29         highest = i;
     30     }
     31 
     32     std::string afterWhite =
     33         afterPrefix.substr(highest + 1, afterPrefix.size());
     34 
     35     int nextWhite = std::min(afterWhite.find(' '), afterWhite.find('\t'));
     36 
     37     if (nextWhite != -1) {
     38         linkText = afterWhite.substr(nextWhite + 1);
     39 
     40         std::string destination = afterWhite.substr(0, nextWhite);
     41 
     42         linkDestination = parseDestination(destination, prior);
     43 
     44     } else {
     45         linkDestination = parseDestination(afterWhite, prior);
     46     }
     47 
     48     if (linkText != std::nullopt) {
     49         std::string tmp = linkText.value();
     50         std::string rdlt = stripLeadingWhiteSpace(tmp);
     51 
     52         renderedText = "[" + std::to_string(linkNumber) + "] " + rdlt;
     53     } else {
     54         std::string tmp = linkDestination.to_string();
     55         std::string rdlt = stripLeadingWhiteSpace(tmp);
     56         renderedText = "[" + std::to_string(linkNumber) + "] " +
     57                        linkDestination.to_string();
     58     }
     59 }
     60 
     61 uri Link::parseDestination(std::string destination, std::optional<uri> prior) {
     62     // TODO: Not sure if this is right w/ how file paths work.
     63 
     64     // TODO: There's a mess here to untangle. Like I think there might be some
     65     // relative links that'll get fucked by this. despite  that, this is a
     66     // requirement to get search parameters working correctly which are probably
     67     // more important.
     68 
     69     if (destination.find(":") == std::string::npos || destination[0] == '?') {
     70         if (prior != std::nullopt) {
     71             // query parameter special casing
     72             // strip old before new.
     73             if (destination.substr(0, 1) == "?") {
     74                 std::string encoded = urlEncode(destination.substr(1));
     75                 std::string base = prior.value().to_string();
     76                 base = base.substr(0, base.find('#'));
     77                 base = base.substr(0, base.find('?'));
     78                 return uri{base + "?" + encoded};
     79             }
     80 
     81             std::string base = prior->get_scheme() + "://" + prior->get_host();
     82 
     83             if (destination[0] == '/') {
     84                 destination = base + destination;
     85             } else {
     86                 std::string path = prior->get_path();
     87                 if (path[0] != '/') {
     88                     path = "/" + path;
     89                 }
     90                 destination =
     91                     base + path.substr(0, path.rfind('/') + 1) + destination;
     92             }
     93         }
     94     }
     95     try {
     96         return uri{destination};
     97     } catch (...) {
     98         invalid = true;
     99         return uri{"gemini://broken-link.com"};  // TODO: Handle this better
    100     }
    101 }
    102 
    103 uri Link::getLinkDestination() { return linkDestination; }
    104 std::optional<std::string> Link::getLinkText() { return linkText; }
    105 
    106 std::string Link::textToDraw() { return renderedText + "\n"; }
    107 
    108 uint8_t Link::getColor() { return COLOR_LINK; }
    109 
    110 LineType Link::type() { return LINK; }
    111 
    112 bool Link::isBold() { return false; }