Skip to content

Commit

Permalink
First working version
Browse files Browse the repository at this point in the history
  • Loading branch information
kelko committed Jul 28, 2022
0 parents commit 1712b75
Show file tree
Hide file tree
Showing 14 changed files with 1,192 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/target
/Cargo.lock
.idea
14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "html-streaming-editor"
version = "0.1.0"
edition = "2021"
authors = [":kelko: <kelko@me.com>"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
"peg" = "0.8.0"
"tl" = "0.7.5"
snafu = { version = "0.7", features = ["backtraces"]}
clap = "3.2"
exitcode = "1.1.2"
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2022 Gregor :kelko: Karzelek

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
HTML Streaming Editor
=======================

Run (simple) manipulations on HTML files, like extracting parts.
Use CSS selectors to define which parts of the HTML to operator on,
use different commands in pipes to perform the desired operations.

Planned commands:
- ONLY: remove everything not matching the CSS selector
- FILTER: remove everything matching the CSS selector
- MAP: run the nodes matching the CSS selector through a sub-pipeline and replace them with the result of that pipeline
- Some attribute & text-content manipulation

Currently supported:
- ONLY
- FILTER is in code, but mis-behaves
57 changes: 57 additions & 0 deletions src/bin/hse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
extern crate clap;

use clap::clap_app;
use html_streaming_editor::HtmlStreamingEditor;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Write};

fn main() {
let options = clap_app!(rodata =>
(version: "0.1")
(author: ":kelko:")
(about: "Html Streaming Editor")
(@arg input: -i --input +takes_value "File name of the Input. `-` for stdin (default)")
(@arg output: -o --output +takes_value "File name of the Output. `-` for stdout (default)")
(@arg COMMANDS: +required "Single string with the command pipeline to perform")
)
.get_matches();

let input_path = options.value_of("input").unwrap_or("-").to_string();
let output_path = options.value_of("output").unwrap_or("-").to_string();
let commands = options
.value_of("COMMANDS")
.expect("COMMANDS must be set")
.to_string();

let input_reader: Box<dyn BufRead> = if input_path == "-" {
Box::new(std::io::stdin().lock())
} else {
let input_file = if let Ok(file) = File::open(input_path) {
file
} else {
eprintln!("Could not open input file");
std::process::exit(exitcode::NOINPUT);
};

Box::new(BufReader::new(input_file))
};

let output_writer: Box<dyn Write> = if output_path == "-" {
Box::new(std::io::stdout().lock())
} else {
let output_file = if let Ok(file) = File::create(output_path) {
file
} else {
eprintln!("Could not open output file");
std::process::exit(exitcode::CANTCREAT);
};

Box::new(BufWriter::new(output_file))
};

let editor = HtmlStreamingEditor::new(input_reader, output_writer);
match editor.run(commands) {
Ok(()) => (),
Err(e) => todo!(),
}
}
55 changes: 55 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use crate::{CssSelectorList, HtmlIndex};
use std::collections::HashSet;
use tl::NodeHandle;

pub enum Command<'a> {
Only(CssSelectorList<'a>),
Filter(CssSelectorList<'a>),
// Map(String, Pipeline),
// GetAttribute(String),
// SetAttribute(String, Pipeline),
// RemoveAttribute(String),
// GetText(),
// SetText(Pipeline),
// RemoveText(),
}

impl<'a> Command<'a> {
pub(crate) fn execute(
&self,
input: &HashSet<NodeHandle>,
index: &'a HtmlIndex<'a>,
) -> Result<HashSet<NodeHandle>, ()> {
match self {
Command::Only(selector) => Self::only(input, index, selector),
Command::Filter(selector) => Self::filter(input, index, selector),
}
}
fn only(
input: &HashSet<NodeHandle>,
index: &'a HtmlIndex<'a>,
selector: &CssSelectorList<'a>,
) -> Result<HashSet<NodeHandle>, ()> {
Ok(selector.query(index, input))
}

fn filter(
input: &HashSet<NodeHandle>,
index: &'a HtmlIndex<'a>,
selector: &CssSelectorList<'a>,
) -> Result<HashSet<NodeHandle>, ()> {
//TODO: Fix. too many nodes returned
let findings = selector.query(index, input);

let parser = index.dom.parser();
for node in findings.iter() {
node.get(parser)
.unwrap()
.inner_html(parser)
.to_mut()
.clear()
}

Ok(input.clone())
}
}
Loading

0 comments on commit 1712b75

Please sign in to comment.