Skip to content

Commit

Permalink
Create mdbook-open-gh-issue from mdbook-open-on-gh
Browse files Browse the repository at this point in the history
  • Loading branch information
vytstank committed Dec 18, 2023
1 parent 24c3c8c commit 1b442eb
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- "*"

env:
CRATE_NAME: mdbook-open-on-gh
CRATE_NAME: mdbook-open-gh-issue

jobs:
# Build sources for every OS
Expand Down
36 changes: 21 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
[package]
name = "mdbook-open-on-gh"
version = "2.4.2"
authors = ["Jan-Erik Rediger <[email protected]>"]
name = "mdbook-open-gh-issue"
version = "0.1.0"
authors = ["Vytautas Stankevičius <[email protected]>"]
description = "mdbook preprocessor to add a open-on-github link on every page"
license = "MPL-2.0"
homepage = "https://github.com/badboy/mdbook-open-on-gh"
repository = "https://github.com/badboy/mdbook-open-on-gh"
homepage = "https://github.com/vytstank/mdbook-open-gh-issue"
repository = "https://github.com/vytstank/mdbook-open-gh-issue"
documentation = "https://github.com/slowsage/mdbook-pagetoc"
readme = "README.md"
keywords = ["mdbook"]
exclude = [".github/"]
edition = "2018"

[dependencies]
mdbook = "0.4.36"
env_logger = "0.10.0"
env_logger = "0.10"
log = "0.4"
clap = { version = "4.1.0", features = ["cargo"] }
serde_json = "1.0"
toml = "0.5.11"
semver = "1.0.20"
34 changes: 16 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
# mdbook-open-on-gh
# mdbook-open-gh-issue

A preprocessor for [mdbook][] to add a open-on-github link on every page.
A preprocessor for [mdbook][] to add a "open github issue link" on every page.

[mdbook]: https://github.com/rust-lang/mdBook

It adds an "Edit this file on GitHub" link on the bottom of every page, linking directly to the source file.
It adds an "Open issue on GitHub for this file" link on the bottom of every page, linking directly to the source file.
It uses the configured `git-repository-url` as the base.

## Installation

If you want to use only this preprocessor, install the tool:

```
cargo install mdbook-open-on-gh
cargo install mdbook-open-gh-issue
```

Add it as a preprocessor to your `book.toml`:

```
[preprocessor.open-on-gh]
command = "mdbook-open-on-gh"
[preprocessor.open-gh-issue]
command = "mdbook-open-gh-issue"
renderer = ["html"]
```

## Configuration

`mdbook-open-on-gh` is configured using additional options under `[output.html]`:

`mdbook-open-gh-issue` is configured using additional options under `[output.html]`:

```toml
[output.html]
# Required: Your repository URL used in the link.
git-repository-url = "https://github.com/$user/$project"

# Your git branch. Defaults to `main`
git-branch = "main"

# The text to use in the footer.
# The link text is marked by `[]`
open-on-text = "Found a bug? [Edit this page on GitHub.]"
gh-issue-text = "Outdated info? [Open issue on GitHub.]"

# The issue template to use. Defaults to "issue-template.yaml"
# New issue will get "file" parameter set to the current page.
gh-issue-template = "issue-template.yaml"
```

To style the footer add a custom CSS file for your HTML output:

```toml
[output.html]
additional-css = ["open-in.css"]
additional-css = ["gh-issue-open.css"]
```

And in `open-in.css` style the `<footer>` element or directly the CSS element id `open-on-gh`:
And in `gh-issue-open.css` style the `<footer>` element or directly the CSS element id `gh-issue-open`:

```css
footer {
Expand All @@ -62,14 +62,12 @@ footer {
This code block shrinks the text size, center-aligns it under the rest of the content
and adds a small horizontal bar above the text to separate it from the page content.


Finally, build your book as normal:

```
mdbook path/to/book
```

## License
## Acknowledgments

MPL. See [LICENSE](LICENSE).
Copyright (c) 2020-2022 Jan-Erik Rediger <[email protected]>
- [mdbook-open-on-gh](https://github.com/badboy/mdbook-open-on-gh)
32 changes: 18 additions & 14 deletions src/bin/mdbook-open-on-gh.rs → src/bin/mdbook-open-gh-issue.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use clap::{crate_version, Arg, ArgMatches, Command};
use mdbook::errors::Error;
use mdbook::preprocess::{CmdPreprocessor, Preprocessor};
use mdbook_open_on_gh::OpenOn;
use mdbook_open_gh_issue::OpenGhIssue;
use semver::{Version, VersionReq};

use std::io;
use std::process;

pub fn make_app() -> Command {
Command::new("mdbook-open-on-gh")
Command::new("mdbook-open-gh-issue")
.version(crate_version!())
.about("mdbook preprocessor to add links to open the page on GitHub")
.about("mdbook preprocessor to add links to open issue on GitHub")
.subcommand(
Command::new("supports")
.arg(Arg::new("renderer").required(true))
Expand All @@ -21,40 +22,43 @@ fn main() {
env_logger::init();

let matches = make_app().get_matches();
let preprocessor = OpenGhIssue::new();

if let Some(sub_args) = matches.subcommand_matches("supports") {
handle_supports(sub_args);
} else if let Err(e) = handle_preprocessing() {
handle_supports(&preprocessor, sub_args);
} else if let Err(e) = handle_preprocessing(&preprocessor) {
eprintln!("{}", e);
process::exit(1);
}
}

fn handle_preprocessing() -> Result<(), Error> {
fn handle_preprocessing(pre: &dyn Preprocessor) -> Result<(), Error> {
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?;

if ctx.mdbook_version != mdbook::MDBOOK_VERSION {
let book_version = Version::parse(&ctx.mdbook_version)?;
let version_req = VersionReq::parse(mdbook::MDBOOK_VERSION)?;

if !version_req.matches(&book_version) {
eprintln!(
"Warning: The mdbook-open-on-gh preprocessor was built against version \
{} of mdbook, but we're being called from version {}",
"Warning: The {} plugin was built against version {} of mdbook, \
but we're being called from version {}",
pre.name(),
mdbook::MDBOOK_VERSION,
ctx.mdbook_version
);
}

let processed_book = OpenOn.run(&ctx, book)?;
let processed_book = pre.run(&ctx, book)?;
serde_json::to_writer(io::stdout(), &processed_book)?;

Ok(())
}

fn handle_supports(sub_args: &ArgMatches) -> ! {
fn handle_supports(pre: &dyn Preprocessor, sub_args: &ArgMatches) -> ! {
let renderer = sub_args
.get_one::<String>("renderer")
.expect("Required argument");
let supported = OpenOn.supports_renderer(renderer);

// Signal whether the renderer is supported by exiting with 1 or 0.
let supported = pre.supports_renderer(renderer);
if supported {
process::exit(0);
} else {
Expand Down
Loading

0 comments on commit 1b442eb

Please sign in to comment.