Guest User

Untitled

a guest
Nov 24th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #[macro_use] extern crate lazy_static;
  2. extern crate regex;
  3.  
  4.  
  5. use std::env;
  6. use std::fs::File;
  7. use std::io::BufReader;
  8. use std::io::prelude::*;
  9.  
  10. use regex::Regex;
  11.  
  12.  
  13. fn environment_regex(env: &str) -> (Regex, Regex) {
  14. let begin_reg_string: String = "[[:space:]]*\\\\begin\\{".to_owned() + env + "\\}[[:space:]]*";
  15. let end_reg_string: String = "[[:space:]]*\\\\end\\{".to_owned() + env + "\\}[[:space:]]*";
  16.  
  17. let begin_regex = Regex::new(&begin_reg_string).unwrap();
  18. let end_regex = Regex::new(&end_reg_string).unwrap();
  19.  
  20. return (begin_regex, end_regex)
  21. }
  22.  
  23. struct Equation {
  24. text: String,
  25. label: String
  26. }
  27.  
  28. fn recognize_label(line: &str) -> Option<(&str, &str)> {
  29. lazy_static! {
  30. static ref RE: Regex = Regex::new(r"[[:space:]]*\\label\{([[:alnum:]-]*):([^\}]*)\}").unwrap();
  31. }
  32.  
  33. let caps = RE.captures(line);
  34. if caps.is_none() {
  35. return None
  36. }
  37. let caps = caps.unwrap();
  38.  
  39. if caps.get(1).is_none() || caps.get(2).is_none() {
  40. return None
  41. } else {
  42. return Some((caps.get(1).unwrap().as_str(), caps.get(2).unwrap().as_str()))
  43. }
  44. }
  45.  
  46.  
  47.  
  48. fn main() {
  49. let args: Vec<String> = env::args().collect();
  50.  
  51. let filename = &args[1];
  52.  
  53. let f = File::open(filename).expect("File not found.");
  54. let f = BufReader::new(f);
  55.  
  56. let (begin_equation, end_equation) = environment_regex("equation");
  57. let (begin_align, end_align) = environment_regex("align");
  58.  
  59. let mut equations: Vec<Equation> = Vec::new();
  60.  
  61. let mut lines_iter = f.lines();
  62.  
  63. 'outer: loop {
  64. let line = lines_iter.next();
  65.  
  66. if line.is_none() {
  67. break;
  68. }
  69.  
  70. // the second unwrap to get the line out of the option
  71. let line = line.unwrap().unwrap();
  72.  
  73. if begin_equation.is_match(&line) {
  74. let mut text = String::new();
  75. let mut label = String::new();
  76.  
  77. loop {
  78. let next_line = lines_iter.next();
  79. if next_line.is_none() {
  80. break 'outer;
  81. }
  82.  
  83. let next_line = next_line.unwrap().unwrap();
  84.  
  85. if end_equation.is_match(&next_line) {
  86. equations.push(
  87. Equation{
  88. text,
  89. label
  90. }
  91. );
  92. break;
  93. }
  94. let label_string = recognize_label(&next_line);
  95.  
  96. if label_string.is_none() {
  97. text += &next_line;
  98. text.push('\n');
  99. } else {
  100. let (kind, key) = label_string.unwrap();
  101. label += key;
  102. }
  103. }
  104. }
  105. }
  106.  
  107. for equation in equations {
  108. println!("== {} ==", equation.label);
  109. println!("{}", equation.text);
  110. }
  111. }
Add Comment
Please, Sign In to add comment