killercup / trpl-ebook

UNMAINTAINED
http://killercup.github.io/trpl-ebook/
478 stars 56 forks source link

Cannot convert correctly for references links #38

Open aaranxu opened 8 years ago

aaranxu commented 8 years ago

When I put the current docs of the official Rust source, I found there were some incorrect results on references link that is [ref] style in the original book, such as some [references]s rather than [references][references]s occurred in the article primitive types.

I have looked up the src of the tool and found the issue was on the /src/helpers/adjust_reference_names.rs that could not dealt with all kinds of references links:

    let reference_link = Regex::new(r"(?x)
        \]\[                # This is a link to a reference
        (?P<id>.+?)         # The reference name
        \]
    ").unwrap();
...
        if reference_link.is_match(line) {
            let new_line = reference_link.replace_all(line, |matches: &Captures| {
                format!("][{prefix}--{id}]",
                    prefix = prefix,
                    id = matches.name("id").expect("no id in ref link")
                )
            });
            return initial + &new_line + "\n";
        }

The code just works on unique style of the references links.

aaranxu commented 8 years ago

I have just tried to modified the source code like as follows, but it hasn't worked yet:

    let reference_link = Regex::new(r"(?x)
        (
            \[
            (?P<words>
                (?:
                \[[^\]]*\]
                |
                [^\[]
            )*
            )
            \]
            \s?
            (?:\n\s*)?
            \[
            (?P<id>.+?)
            \]
        )|(
            (?P<head>
                (?:[^\]]|^)\s?(?:\n\s*)?
            )
            \[
            (?P<wordsaa>
                [^\[\]]+
            )
            \]
            (?P<tail>
                (?:\s{1}(?:[^\[\(]|$))
            )
        )
    ").unwrap();

    let reference_link_shortcut = Regex::new(r"(?x)
        (?P<head>
            (?:[^\]]|^)\s?(?:\n\s*)?
        )
        \[
        (?P<id>
            [^\[\]]+
        )
        \]
        (?P<tail>
            (?:\s?(?:[^\[\(]|$))
        )
    ").unwrap();

See the links format of Markdown.