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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use super::{primary_heading::PrimaryHeading, Readme};
use crate::blacklist::{is_badge_text, is_badge_url};
use gh_api::get_token;
use scraper::ElementRef;
use serde::{Deserialize, Serialize};
use std::{
  cmp::Ordering,
  collections::{HashMap, HashSet},
};
use url::Url;

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum ProjectLink {
  Website,
  Repo,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum KeywordMention {
  Logo,
  Banner,
  RepoName,
}

#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct ReadmeImage {
  pub src: Url,
  pub headers: HashMap<String, String>,
  /// whether the image was in the primary markdown heading
  pub in_primary_heading: bool,
  /// whether the image was the first/last one in the heading
  pub edge_of_primary_heading: bool,
  /// whether the image mentions a keyword in its src / alt text
  pub keyword_mentions: HashSet<KeywordMention>,
  /// whether the image src points to a file inside of the repo
  pub sourced_from_repo: bool,
  /// whether the image has links to the projects
  pub links_to: Option<ProjectLink>,
  /// whether the image has the CSS "align: center"
  pub is_align_center: bool,
  /// whether the image has height or width attributes
  pub has_size_attrs: bool,
}

impl ReadmeImage {
  pub async fn get(
    readme: &Readme,
    elem_ref: &ElementRef<'_>,
    primary_heading: &mut PrimaryHeading<'_>,
  ) -> Option<Self> {
    let elem = elem_ref.value();

    let src = elem
      .attr("data-canonical-src")
      .or(elem.attr("src"))
      .and_then(|src| readme.qualify_url(src).ok())
      .unwrap();

    let alt = elem
      .attr("alt")
      .map(|alt| alt.to_lowercase())
      .unwrap_or(String::new());

    if is_badge_url(&src) || is_badge_text(&alt) {
      return None;
    }

    let cdn_src = elem
      .attr("data-canonical-src")
      .and(elem.attr("src"))
      .and_then(|src| readme.qualify_url(src).ok());

    let mut is_align_center = false;
    let mut links_to = None;
    for elem_ref in elem_ref.ancestors().map(ElementRef::wrap).flatten() {
      let element = elem_ref.value();

      if element.attr("align") == Some("center") {
        is_align_center = true;
      }

      if element.name() == "a" && links_to.is_none() {
        links_to = match element
          .attr("href")
          .and_then(|href| readme.qualify_url(href).ok())
        {
          Some(href) => {
            // if the img points to the same url as the link
            // then its a default url generated by github
            let mut img_blob_url = src.clone();
            img_blob_url.set_path(&src.path().replacen("/raw/", "/blob/", 1));
            if href != img_blob_url {
              readme.is_link_to_project(&href).await
            } else {
              None
            }
          }
          None => None,
        };
      }
    }

    let branch_and_path = readme.get_branch_and_path(&src).await;
    let keyword_mentions = {
      let mut mentions = HashSet::new();

      let mut path = &src.path().to_lowercase();
      if let Some((_, file_path)) = &branch_and_path {
        path = file_path;
      }

      if path.contains("logo") || alt.contains("logo") {
        mentions.insert(KeywordMention::Logo);
      }

      if path.contains("banner") || alt.contains("banner") {
        mentions.insert(KeywordMention::Banner);
      }

      if path.contains(&readme.repo) || alt.contains(&readme.repo) {
        mentions.insert(KeywordMention::RepoName);
      };
      mentions
    };

    let mut headers = HashMap::new();

    let src = cdn_src.unwrap_or({
      if let Some((branch, path)) = &branch_and_path {
        if readme.private {
          headers.insert(
            "Authorization".to_string(),
            format!("Bearer {}", get_token().unwrap()).to_string(),
          );
        }

        Url::parse(&format!(
          "https://raw.githubusercontent.com/{}/{}/{}/{}",
          readme.owner, readme.repo, branch, path
        ))
        .unwrap()
      } else {
        src
      }
    });

    Some(ReadmeImage {
      src,
      headers,
      in_primary_heading: primary_heading.contains(elem_ref),
      edge_of_primary_heading: false,
      keyword_mentions,
      sourced_from_repo: branch_and_path.is_some(),
      links_to,
      is_align_center,
      has_size_attrs: elem.attr("width").or(elem.attr("height")).is_some(),
    })
  }

  pub fn weight(&self) -> u8 {
    let mut weight = 0;

    if self.in_primary_heading {
      weight += 2;

      if self.is_align_center {
        weight += 2;
      }

      if self.has_size_attrs {
        weight += 2;
      }

      if self.sourced_from_repo {
        weight += 4;
      }
    };

    if self.edge_of_primary_heading {
      weight += 4;
    }

    match self.links_to {
      Some(ProjectLink::Website) => {
        weight += 8;
      }
      Some(ProjectLink::Repo) => {
        weight += 4;
      }
      None => {}
    }

    if self.keyword_mentions.contains(&KeywordMention::Logo) {
      weight += 16
    }

    if self.keyword_mentions.contains(&KeywordMention::Banner) {
      weight += 8
    }

    if self.keyword_mentions.contains(&KeywordMention::RepoName) {
      weight += 4
    }

    weight
  }
}

impl Ord for ReadmeImage {
  fn cmp(&self, other: &Self) -> Ordering {
    other.weight().cmp(&self.weight())
  }
}

impl PartialOrd for ReadmeImage {
  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
    Some(self.cmp(other))
  }
}