Skip to content

add option -no-indent to remove indentation #1856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions src/preprocess/links.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::errors::*;
use crate::utils::{
take_anchored_lines, take_lines, take_rustdoc_include_anchored_lines,
take_rustdoc_include_lines,
take_anchored_lines, take_format_remove_indent, take_lines,
take_rustdoc_include_anchored_lines, take_rustdoc_include_lines,
};
use regex::{CaptureMatches, Captures, Regex};
use std::fs;
Expand Down Expand Up @@ -136,6 +136,7 @@ where
enum LinkType<'a> {
Escaped,
Include(PathBuf, RangeOrAnchor),
IncludeNoIndent(PathBuf, RangeOrAnchor),
Playground(PathBuf, Vec<&'a str>),
RustdocInclude(PathBuf, RangeOrAnchor),
Title(&'a str),
Expand Down Expand Up @@ -207,6 +208,7 @@ impl<'a> LinkType<'a> {
match self {
LinkType::Escaped => None,
LinkType::Include(p, _) => Some(return_relative_path(base, &p)),
LinkType::IncludeNoIndent(p, _) => Some(return_relative_path(base, &p)),
LinkType::Playground(p, _) => Some(return_relative_path(base, &p)),
LinkType::RustdocInclude(p, _) => Some(return_relative_path(base, &p)),
LinkType::Title(_) => None,
Expand Down Expand Up @@ -255,9 +257,13 @@ fn parse_include_path(path: &str) -> LinkType<'static> {
let mut parts = path.splitn(2, ':');

let path = parts.next().unwrap().into();
let is_indented = parts.clone().next().unwrap_or("").rsplit(":").next();
let range_or_anchor = parse_range_or_anchor(parts.next());

LinkType::Include(path, range_or_anchor)
match is_indented {
Some(_some) if _some.eq("-no-indent") => LinkType::IncludeNoIndent(path, range_or_anchor),
_ => LinkType::Include(path, range_or_anchor),
}
}

fn parse_rustdoc_include_path(path: &str) -> LinkType<'static> {
Expand Down Expand Up @@ -344,6 +350,23 @@ impl<'a> Link<'a> {
)
})
}
LinkType::IncludeNoIndent(ref pat, ref range_or_anchor) => {
let target = base.join(pat);

fs::read_to_string(&target)
.map(|s| match range_or_anchor {
RangeOrAnchor::Range(range) => take_lines(&s, range.clone()),
RangeOrAnchor::Anchor(anchor) => take_anchored_lines(&s, anchor),
})
.with_context(|| {
format!(
"Could not read file for link {} ({})",
self.link_text,
target.display(),
)
})
.map(|s| take_format_remove_indent(s.as_str()))
}
LinkType::RustdocInclude(ref pat, ref range_or_anchor) => {
let target = base.join(pat);

Expand Down Expand Up @@ -665,6 +688,25 @@ mod tests {
);
}

#[test]
fn test_find_links_with_anchor_no_indent() {
let s = "Some random text with {{#include file.rs:anchor:-no-indent}}...";
let res = find_links(s).collect::<Vec<_>>();
println!("\nOUTPUT: {:?}\n", res);
assert_eq!(
res,
vec![Link {
start_index: 22,
end_index: 60,
link_type: LinkType::IncludeNoIndent(
PathBuf::from("file.rs"),
RangeOrAnchor::Anchor(String::from("anchor"))
),
link_text: "{{#include file.rs:anchor:-no-indent}}",
}]
);
}

#[test]
fn test_find_links_escaped_link() {
let s = "Some random text with escaped playground \\{{#playground file.rs editable}} ...";
Expand Down
4 changes: 2 additions & 2 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use std::fmt::Write;
use std::path::Path;

pub use self::string::{
take_anchored_lines, take_lines, take_rustdoc_include_anchored_lines,
take_rustdoc_include_lines,
take_anchored_lines, take_format_remove_indent, take_lines,
take_rustdoc_include_anchored_lines, take_rustdoc_include_lines,
};

/// Replaces multiple consecutive whitespace characters with a single space character.
Expand Down
26 changes: 26 additions & 0 deletions src/utils/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,32 @@ pub fn take_rustdoc_include_anchored_lines(s: &str, anchor: &str) -> String {
output
}

pub fn take_format_remove_indent(str: &str) -> String {
let mut output = Vec::<String>::new();
let mut min_indent = usize::MAX;

for line in str.lines() {
for (index, c) in line.chars().enumerate() {
if !c.is_whitespace() && min_indent > index {
min_indent = index;
break;
}
}
}

for line in str.lines() {
let formatted: String = line
.chars()
.take(0)
.chain(line.chars().skip(min_indent))
.collect();

output.push(formatted);
}

output.join("\n")
}

#[cfg(test)]
mod tests {
use super::{
Expand Down