@@ -134,7 +134,7 @@ fn fetch_h_entry_post(document: Handle, url: &str) -> eyre::Result<Option<FetchP
134134 } ;
135135 info ! ( "found h-entry post" ) ;
136136
137- let mut base_href = Url :: parse ( & url) ?;
137+ let mut base_href = Url :: parse ( url) ?;
138138 for node in BreadthTraverse :: elements ( document) {
139139 let NodeData :: Element { name, attrs, .. } = & node. data else {
140140 unreachable ! ( )
@@ -191,10 +191,8 @@ fn fetch_h_entry_post(document: Handle, url: &str) -> eyre::Result<Option<FetchP
191191 while let Some ( weak) = node. parent . take ( ) {
192192 let parent = weak. upgrade ( ) . expect ( "dangling weak pointer" ) ;
193193 node. parent . set ( Some ( weak) ) ;
194- if has_class ( parent. clone ( ) , "h-entry" ) ? {
195- if !Rc :: ptr_eq ( & parent, & h_entry) {
196- continue ' category;
197- }
194+ if has_class ( parent. clone ( ) , "h-entry" ) ? && !Rc :: ptr_eq ( & parent, & h_entry) {
195+ continue ' category;
198196 }
199197 node = parent;
200198 }
@@ -239,10 +237,10 @@ async fn fetch_akkoma_post(
239237 let NodeData :: Element { name, attrs, .. } = & node. data else {
240238 unreachable ! ( )
241239 } ;
242- if name == & QualName :: html ( "script" ) {
243- if attrs. borrow ( ) . attr_str ( "id" ) ? == Some ( "initial-results" ) {
244- return Ok ( Some ( serde_json :: from_str ( & text_content ( node ) ? ) ? ) ) ;
245- }
240+ if name == & QualName :: html ( "script" )
241+ && attrs. borrow ( ) . attr_str ( "id" ) ? == Some ( "initial-results" )
242+ {
243+ return Ok ( Some ( serde_json :: from_str ( & text_content ( node ) ? ) ? ) ) ;
246244 }
247245 }
248246 Ok ( None )
@@ -262,7 +260,7 @@ async fn fetch_akkoma_post(
262260 let status_id = fetched_page_url
263261 . path_segments ( )
264262 . ok_or_eyre ( "bad page url" ) ?
265- . last ( )
263+ . next_back ( )
266264 . ok_or_eyre ( "page url has no last path segment" ) ?;
267265 trace ! ( ?status_id) ;
268266 let api_url = instance_url. join ( & format ! ( "api/v1/statuses/{status_id}" ) ) ?;
@@ -304,7 +302,7 @@ async fn fetch_akkoma_post(
304302
305303 Ok ( Some ( FetchPostResult {
306304 base_href : url. clone ( ) ,
307- content : content ,
305+ content,
308306 url,
309307 meta,
310308 } ) )
@@ -349,75 +347,66 @@ fn process_content(
349347 let dom = parse_html_fragment ( content. as_bytes ( ) ) ?;
350348
351349 for node in BreadthTraverse :: nodes ( dom. document . clone ( ) ) {
352- match & node. data {
353- NodeData :: Element { name, attrs, .. } => {
354- let mut attrs = attrs. borrow_mut ( ) ;
355- let mut extra_attrs = vec ! [ ] ;
356- if let Some ( attr_names) = html_attributes_with_embedding_urls ( ) . get ( name) {
357- for attr in attrs. iter_mut ( ) {
358- if attr_names. contains ( & attr. name ) {
359- // rewrite attachment urls to relative cached paths.
360- let old_url = attr. value . to_str ( ) . to_owned ( ) ;
361- let fetch_url = base_href. join ( & old_url) ?;
362- trace ! (
363- "found attachment url in <{} {}>: {old_url}" ,
364- name. local,
365- attr. name. local
366- ) ;
367- attr. value = context
368- . cache_imported ( & fetch_url. to_string ( ) , post_basename) ?
369- . site_path ( ) ?
370- . base_relative_url ( )
371- . into ( ) ;
372- extra_attrs. push ( Attribute {
373- name : QualName :: attribute ( & format ! (
374- "data-import-{}" ,
375- attr. name. local
376- ) ) ,
377- value : old_url. into ( ) ,
378- } ) ;
379- }
350+ if let NodeData :: Element { name, attrs, .. } = & node. data {
351+ let mut attrs = attrs. borrow_mut ( ) ;
352+ let mut extra_attrs = vec ! [ ] ;
353+ if let Some ( attr_names) = html_attributes_with_embedding_urls ( ) . get ( name) {
354+ for attr in attrs. iter_mut ( ) {
355+ if attr_names. contains ( & attr. name ) {
356+ // rewrite attachment urls to relative cached paths.
357+ let old_url = attr. value . to_str ( ) . to_owned ( ) ;
358+ let fetch_url = base_href. join ( & old_url) ?;
359+ trace ! (
360+ "found attachment url in <{} {}>: {old_url}" ,
361+ name. local,
362+ attr. name. local
363+ ) ;
364+ attr. value = context
365+ . cache_imported ( fetch_url. as_ref ( ) , post_basename) ?
366+ . site_path ( ) ?
367+ . base_relative_url ( )
368+ . into ( ) ;
369+ extra_attrs. push ( Attribute {
370+ name : QualName :: attribute ( & format ! ( "data-import-{}" , attr. name. local) ) ,
371+ value : old_url. into ( ) ,
372+ } ) ;
380373 }
381374 }
382- if let Some ( attr_names) = html_attributes_with_non_embedding_urls ( ) . get ( name) {
383- for attr in attrs. iter_mut ( ) {
384- if attr_names. contains ( & attr. name ) {
385- // rewrite urls in links to bake in the `base_href`.
386- let old_url = attr. value . to_str ( ) . to_owned ( ) ;
387- let new_url = if old_url. starts_with ( "#" ) {
388- format ! ( "#user-content-{}" , & old_url[ 1 ..] )
389- } else {
390- base_href. join ( & old_url) ?. to_string ( )
391- } ;
392- trace ! (
393- "rewriting <{} {}>: {old_url:?} -> {new_url:?}" ,
394- name. local,
395- attr. name. local,
396- ) ;
397- attr. value = new_url. to_string ( ) . into ( ) ;
398- extra_attrs. push ( Attribute {
399- name : QualName :: attribute ( & format ! (
400- "data-import-{}" ,
401- attr. name. local
402- ) ) ,
403- value : old_url. into ( ) ,
404- } ) ;
405- }
375+ }
376+ if let Some ( attr_names) = html_attributes_with_non_embedding_urls ( ) . get ( name) {
377+ for attr in attrs. iter_mut ( ) {
378+ if attr_names. contains ( & attr. name ) {
379+ // rewrite urls in links to bake in the `base_href`.
380+ let old_url = attr. value . to_str ( ) . to_owned ( ) ;
381+ let new_url = if let Some ( fragment) = old_url. strip_prefix ( "#" ) {
382+ format ! ( "#user-content-{fragment}" )
383+ } else {
384+ base_href. join ( & old_url) ?. to_string ( )
385+ } ;
386+ trace ! (
387+ "rewriting <{} {}>: {old_url:?} -> {new_url:?}" ,
388+ name. local,
389+ attr. name. local,
390+ ) ;
391+ attr. value = new_url. to_string ( ) . into ( ) ;
392+ extra_attrs. push ( Attribute {
393+ name : QualName :: attribute ( & format ! ( "data-import-{}" , attr. name. local) ) ,
394+ value : old_url. into ( ) ,
395+ } ) ;
406396 }
407397 }
408- if name == & QualName :: html ( "img" ) {
409- extra_attrs. push ( Attribute {
410- name : QualName :: attribute ( "loading" ) ,
411- value : "lazy" . into ( ) ,
412- } ) ;
413- }
414- attrs. extend ( extra_attrs) ;
415398 }
416- _ => { }
399+ if name == & QualName :: html ( "img" ) {
400+ extra_attrs. push ( Attribute {
401+ name : QualName :: attribute ( "loading" ) ,
402+ value : "lazy" . into ( ) ,
403+ } ) ;
404+ }
405+ attrs. extend ( extra_attrs) ;
417406 }
418407 }
419408
420- Ok ( serialize_html_fragment ( dom) ? )
409+ serialize_html_fragment ( dom)
421410}
422411
423412fn mf2_e ( node : Handle , class : & str ) -> eyre:: Result < Option < String > > {
@@ -519,7 +508,7 @@ fn mf2_find_all(node: Handle, class: &str) -> Vec<Handle> {
519508fn has_class ( node : Handle , class : & str ) -> eyre:: Result < bool > {
520509 if let NodeData :: Element { attrs, .. } = & node. data {
521510 if let Some ( node_class) = attrs. borrow ( ) . attr_str ( "class" ) ? {
522- if node_class. split ( " " ) . find ( | & c| c == class) . is_some ( ) {
511+ if node_class. split ( " " ) . any ( | c| c == class) {
523512 return Ok ( true ) ;
524513 }
525514 }
0 commit comments