Function tpnote_lib::html::rewrite_links

source ·
pub fn rewrite_links(
    html: String,
    root_path: &Path,
    docdir: &Path,
    local_link_kind: LocalLinkKind,
    rewrite_ext: bool,
    allowed_local_links: Arc<RwLock<HashSet<PathBuf>>>
) -> String
Expand description

Helper function that scans the input html string and converts all relative local HTML links to absolute links.

The base path for this conversion (usually where the HTML file resides), is docdir. If not rewrite_rel_links, relative local links are not converted. Furthermore, all local absolute (not converted) links are prepended with root_path. All external URLs always remain untouched. If rewrite_abs_links and link is absolute, concatenate and return root_path and dest. If rewrite_ext is true and the link points to a known Tp-Note file extension, then .html is appended to the converted link. Remark: The anchor’s text property is never changed. However, there is one exception: when the text contains a URL starting with http: or https:, only the file stem is kept. Example, the anchor text property: <a ...>http:dir/my file.md</a> is rewritten into <a ...>my file</a>.

It is guaranteed, that all local links in the converted html point inside root_path. If not, the link is displayed as INVALID LOCAL LINK and discarded. All valid local links are inserted in allowed_local_links the same way as their destination appears in the resulting HTML.

Examples found in repository?
src/note.rs (lines 423-431)
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
    pub fn export_html(
        &self,
        html_template: &str,
        export_dir: &Path,
        local_link_kind: LocalLinkKind,
    ) -> Result<(), NoteError> {
        // Determine filename of html-file.
        let mut html_path = PathBuf::new();
        let current_path = if self.rendered_filename != PathBuf::new() {
            &self.rendered_filename
        } else {
            &self.context.path
        };

        let current_dir_path = current_path.parent().unwrap_or_else(|| Path::new(""));

        if export_dir
            .as_os_str()
            .to_str()
            .unwrap_or_default()
            .is_empty()
        {
            html_path = current_path
                .parent()
                .unwrap_or_else(|| Path::new(""))
                .to_path_buf();
            let mut html_filename = current_path
                .file_name()
                .unwrap_or_default()
                .to_str()
                .unwrap_or_default()
                .to_string();
            html_filename.push_str(".html");
            html_path.push(PathBuf::from(html_filename.as_str()));
        } else if export_dir.display().to_string() != "-" {
            html_path = export_dir.to_owned();
            let mut html_filename = current_path
                .file_name()
                .unwrap_or_default()
                .to_str()
                .unwrap_or_default()
                .to_string();
            html_filename.push_str(HTML_EXT);
            html_path.push(PathBuf::from(html_filename.as_str()));
        } else {
            // `export_dir` points to `-` and `html_path` is empty.
        }

        if html_path
            .as_os_str()
            .to_str()
            .unwrap_or_default()
            .is_empty()
        {
            log::info!("Rendering HTML to STDOUT (`{:?}`)", export_dir);
        } else {
            log::info!("Rendering HTML into: {:?}", html_path);
        };

        // The file extension identifies the markup language.
        let note_path_ext = current_path
            .extension()
            .unwrap_or_default()
            .to_str()
            .unwrap_or_default()
            .to_string();

        // Check where to dump output.
        if html_path
            .as_os_str()
            .to_str()
            .unwrap_or_default()
            .is_empty()
        {
            let stdout = io::stdout();
            let mut handle = stdout.lock();

            // Write HTML rendition.
            handle.write_all(
                self.render_content_to_html(&note_path_ext, html_template)
                    .map(|html| {
                        rewrite_links(
                            html,
                            &self.context.root_path,
                            current_dir_path,
                            local_link_kind,
                            // Do append `.html` to `.md` in links.
                            true,
                            Arc::new(RwLock::new(HashSet::new())),
                        )
                    })?
                    .as_bytes(),
            )?;
        } else {
            let mut handle = OpenOptions::new()
                .write(true)
                .create(true)
                .open(&html_path)?;
            // Write HTML rendition.
            handle.write_all(
                self.render_content_to_html(&note_path_ext, html_template)
                    .map(|html| {
                        rewrite_links(
                            html,
                            &self.context.root_path,
                            current_dir_path,
                            local_link_kind,
                            // Do append `.html` to `.md` in links.
                            true,
                            Arc::new(RwLock::new(HashSet::new())),
                        )
                    })?
                    .as_bytes(),
            )?;
        };
        Ok(())
    }