Skip to content
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

Transforming input before reporting #1067

Open
wants to merge 4 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
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ pub struct Config {
#[serde(skip)]
/// adds a closure for each function, e.g., print the result
pub for_each_fn: Option<fn(LanguageType, Report)>,
#[serde(skip)]
/// function to transform the file content before analysis
pub transform_fn: Option<fn(&[u8], &LanguageType) -> String>,
}


impl Config {
/// Constructs a new `Config` from either `$base/tokei.toml` or
/// `$base/.tokeirc`. `tokei.toml` takes precedence over `.tokeirc`
Expand Down
41 changes: 23 additions & 18 deletions src/language/language_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ include!(concat!(env!("OUT_DIR"), "/language_type.rs"));

impl Serialize for LanguageType {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer {
where
S: serde::Serializer,
{
serializer.serialize_str(self.name())
}
}
Expand Down Expand Up @@ -63,7 +64,16 @@ impl LanguageType {

/// Parses the bytes provided as the given [`LanguageType`].
pub fn parse_from_slice<A: AsRef<[u8]>>(self, text: A, config: &Config) -> CodeStats {
let text = text.as_ref();
let mut text = text.as_ref();
let func = config.transform_fn;
let transformed: String;
text = match func {
Some(f) => {
transformed = f(text, &self);
transformed.as_bytes()
}
_ => text,
};

if self == LanguageType::Jupyter {
return self
Expand All @@ -73,21 +83,16 @@ impl LanguageType {

let syntax = SyntaxCounter::new(self);

if let Some(end) = syntax
.shared
.important_syntax
.find(text)
.and_then(|m| {
// Get the position of the last line before the important
// syntax.
text[..=m.start()]
.iter()
.rev()
.position(|&c| c == b'\n')
.filter(|&p| p != 0)
.map(|p| m.start() - p)
})
{
if let Some(end) = syntax.shared.important_syntax.find(text).and_then(|m| {
// Get the position of the last line before the important
// syntax.
text[..=m.start()]
.iter()
.rev()
.position(|&c| c == b'\n')
.filter(|&p| p != 0)
.map(|p| m.start() - p)
}) {
let (skippable_text, rest) = text.split_at(end + 1);
let is_fortran = syntax.shared.is_fortran;
let is_literate = syntax.shared.is_literate;
Expand Down
Loading