diff --git a/.gitignore b/.gitignore index ba6f3d7..47bcc6b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.v *.py *.inst +!*_preamble.py # macOS .DS_Store diff --git a/examples/D1_preamble.py b/examples/D1_preamble.py new file mode 100644 index 0000000..661e91b --- /dev/null +++ b/examples/D1_preamble.py @@ -0,0 +1,2 @@ +if_rst = True +if_en = True diff --git a/src/config.rs b/src/config.rs index 99c3994..75eb3d2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -83,6 +83,9 @@ struct Args { /// Variables (multiple occurrences allowed) #[arg(short, long = "var", value_name = "KEY=VAL")] vars: Vec, + /// Preamble Python file + #[arg(short, long = "preamble", value_name = "FILE", required = false)] + preamble_py: String } impl Config { @@ -113,7 +116,7 @@ impl Config { } /// Parses the command line arguments and returns a tuple of `Config` and `FileOptions`. - pub fn from_args() -> (Config, FileOptions, Option>) { + pub fn from_args() -> (Config, FileOptions, Option>, Option) { let args = Args::parse(); let vars = args .vars @@ -137,6 +140,11 @@ impl Config { output: args.output, }, vars.ok(), + if args.preamble_py.is_empty() { + None + } else { + Some(args.preamble_py) + } ) } diff --git a/src/convert.rs b/src/convert.rs index ca0d283..4fa9a3e 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -14,6 +14,7 @@ pub struct Convert { config: Config, file_options: FileOptions, vars: Option>, + preamble_py: Option, } #[derive(Debug, PartialEq)] @@ -32,18 +33,24 @@ impl Default for LineType { impl Convert { /// Creates a new `Convert` instance with the given configuration and file options. - pub fn new(config: Config, file_options: FileOptions, vars: Option>) -> Convert { + pub fn new( + config: Config, + file_options: FileOptions, + vars: Option>, + preamble_py: Option, + ) -> Convert { Convert { config, file_options, vars, + preamble_py, } } /// Creates a new `Convert` instance by parsing command line arguments. pub fn from_args() -> Convert { - let (config, file_options, vars) = Config::from_args(); - Convert::new(config, file_options, vars) + let (config, file_options, vars, preamble_py) = Config::from_args(); + Convert::new(config, file_options, vars, preamble_py) } /// Opens the input file and reads its contents as a string. @@ -246,11 +253,19 @@ impl Convert { #[cfg(feature = "inst")] // print user-defined variables if let Some(vars) = &self.vars { - writeln!(stream, "# User-defined variables:")?; - for (name, value) in vars { - writeln!(stream, "{} = {}", name, value)?; + if !vars.is_empty() { + writeln!(stream, "# User-defined variables:")?; + for (name, value) in vars { + writeln!(stream, "{} = {}", name, value)?; + } + writeln!(stream)?; } - writeln!(stream)?; + } + // load preamble + if let Some(preamble_py) = &self.preamble_py { + // read from file and write to stream + let preamble_py = std::fs::read_to_string(preamble_py)?; + writeln!(stream, "# Preamble:\n{}", preamble_py)?; } writeln!( stream, @@ -303,7 +318,8 @@ impl Convert { py_indent_prior, &line ))?; } - py_indent_space = self.update_py_indent_space(&line, py_indent_space) - py_indent_prior; + py_indent_space = + self.update_py_indent_space(&line, py_indent_space) - py_indent_prior; #[cfg(feature = "inst")] self.process_python_line( &line,