d-0004-mdbook-2-1-compat
authorDebian Rust Maintainers <pkg-rust-maintainers@alioth-lists.debian.net>
Thu, 5 Sep 2019 07:06:23 +0000 (08:06 +0100)
committerXimin Luo <infinity0@debian.org>
Thu, 5 Sep 2019 07:06:23 +0000 (08:06 +0100)
Gbp-Pq: Name d-0004-mdbook-2-1-compat.patch

src/tools/rustbook/Cargo.toml
src/tools/rustbook/src/main.rs
vendor/mdbook/src/renderer/html_handlebars/hbs_renderer.rs
vendor/mdbook/src/utils/mod.rs

index 54549e4c7e98fab690f058713d7bb3e902ed8b5b..a8d1ab9d97252581dc4e8087280734d1d0ff526d 100644 (file)
@@ -12,9 +12,3 @@ clap = "2.25.0"
 version = "0.3.0"
 default-features = false
 features = ["search"]
-
-[dependencies.mdbook_1]
-package = "mdbook"
-version = "0.1.7"
-default-features = false
-features = ["search"]
index 04e48dde86b215af2761a073653506eb3737b39d..b12d781667ec20c85df5cdb4375fa1be5157d55b 100644 (file)
@@ -7,9 +7,6 @@ use std::path::{Path, PathBuf};
 
 use clap::{App, ArgMatches, SubCommand, AppSettings};
 
-use mdbook_1::{MDBook as MDBook1};
-use mdbook_1::errors::{Result as Result1};
-
 use mdbook::MDBook;
 use mdbook::errors::Result;
 
@@ -37,18 +34,11 @@ fn main() {
     match matches.subcommand() {
         ("build", Some(sub_matches)) => {
             match sub_matches.value_of("mdbook-vers") {
-                None | Some("1") => {
-                    if let Err(e) = build_1(sub_matches) {
-                        eprintln!("Error: {}", e);
-
-                        for cause in e.iter().skip(1) {
-                            eprintln!("\tCaused By: {}", cause);
-                        }
-
-                        ::std::process::exit(101);
+                None | Some("1") | Some("2") | Some("3") => {
+                    match sub_matches.value_of("mdbook-vers") {
+                      Some("1") => env::set_var("DEB_MDBOOK_1_COMPAT", "1"),
+                      _ => (),
                     }
-                }
-                Some("2") | Some("3") => {
                     if let Err(e) = build(sub_matches) {
                         eprintln!("Error: {}", e);
 
@@ -68,23 +58,6 @@ fn main() {
     };
 }
 
-// Build command implementation
-pub fn build_1(args: &ArgMatches<'_>) -> Result1<()> {
-    let book_dir = get_book_dir(args);
-    let mut book = MDBook1::load(&book_dir)?;
-
-    // Set this to allow us to catch bugs in advance.
-    book.config.build.create_missing = false;
-
-    if let Some(dest_dir) = args.value_of("dest-dir") {
-        book.config.build.build_dir = PathBuf::from(dest_dir);
-    }
-
-    book.build()?;
-
-    Ok(())
-}
-
 // Build command implementation
 pub fn build(args: &ArgMatches<'_>) -> Result<()> {
     let book_dir = get_book_dir(args);
index e5cb073388a767e0fe794291c5b6e1518fbf65f2..1cbcfec7a1cca84c37b772f2023edfc19f5441d7 100644 (file)
@@ -10,6 +10,7 @@ use std::collections::BTreeMap;
 use std::collections::HashMap;
 use std::fs;
 use std::path::{Path, PathBuf};
+use std::env;
 
 use handlebars::Handlebars;
 use regex::{Captures, Regex};
@@ -66,7 +67,7 @@ impl HtmlHandlebars {
             }
 
             ctx.data.insert("path".to_owned(), json!(path));
-            ctx.data.insert("content".to_owned(), json!(content));
+            ctx.data.insert("content".to_owned(), json!(if env::var("DEB_MDBOOK_1_COMPAT") == Ok("1".to_string()) { fixed_content } else { content }));
             ctx.data.insert("chapter_title".to_owned(), json!(ch.name));
             ctx.data.insert("title".to_owned(), json!(title));
             ctx.data.insert(
index 0f199fb729aea40e4905bc96b632df3cc552a650..1a86bf8a7bab13fab47d2309c54bd2f02cbaa5d1 100644 (file)
@@ -4,6 +4,8 @@ pub mod fs;
 mod string;
 use crate::errors::Error;
 use regex::Regex;
+use std::path::Path;
+use std::env;
 
 use pulldown_cmark::{html, CowStr, Event, Options, Parser, Tag};
 
@@ -69,11 +71,13 @@ fn adjust_links<'a>(event: Event<'a>, with_base: &str) -> Event<'a> {
     lazy_static! {
         static ref SCHEME_LINK: Regex = Regex::new(r"^[a-z][a-z0-9+.-]*:").unwrap();
         static ref MD_LINK: Regex = Regex::new(r"(?P<link>.*)\.md(?P<anchor>#.*)?").unwrap();
+        static ref HTML_LINK: Regex = Regex::new(r"(?P<link>.*)\.html(?P<anchor>#.*)?").unwrap();
     }
 
     fn fix<'a>(dest: CowStr<'a>, base: &str) -> CowStr<'a> {
         // Don't modify links with schemes like `https`.
         if !SCHEME_LINK.is_match(&dest) {
+            let old_dest = &dest;
             // This is a relative link, adjust it as necessary.
             let mut fixed_link = String::new();
             if !base.is_empty() {
@@ -88,6 +92,27 @@ fn adjust_links<'a>(event: Event<'a>, with_base: &str) -> Event<'a> {
                     fixed_link.push_str(anchor.as_str());
                 }
             } else {
+                // compatibility for mdbook 1
+                if env::var("DEB_MDBOOK_1_COMPAT") == Ok("1".to_string()) {
+                    if let Some(caps) = HTML_LINK.captures(&old_dest) {
+                        let base = Path::new(base);
+                        let mut path = Vec::new();
+                        for _ in base.components() {
+                            path.push("../");
+                        }
+                        path.extend(&[&caps["link"], ".html"]);
+                        let mut html_link = path.concat();
+
+                        if let Some(anchor) = caps.name("anchor") {
+                            html_link.push_str(anchor.as_str().replace("#a--", "#--").as_str());
+                        }
+
+                        return CowStr::from(html_link);
+                    } else {
+                        return dest;
+                    }
+                }
+
                 fixed_link.push_str(&dest);
             };
             return CowStr::from(fixed_link);