Skip to content

Commit

Permalink
Merge pull request #13 from sean0x42/v2
Browse files Browse the repository at this point in the history
Release markdown extract v2
  • Loading branch information
sean0x42 authored Jan 16, 2022
2 parents 6fb5691 + 15196cc commit e923cc4
Show file tree
Hide file tree
Showing 21 changed files with 370 additions and 355 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target/
/.git/
12 changes: 0 additions & 12 deletions .github/FUNDING.yml

This file was deleted.

19 changes: 19 additions & 0 deletions .github/scripts/extract_notes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

if [[ -z $RELEASE_NAME ]]; then
echo "Missing env var RELEASE_NAME"
exit 1
fi

pattern="^$RELEASE_NAME"

notes=$(docker run -v $PWD:/opt -it sean0x42/markdown-extract:v2 \
--no-print-matched-heading \
$pattern \
/opt/CHANGELOG.md)

notes=="${notes=//'%'/'%25'}"
notes=="${notes=//$'\n'/'%0A'}"
notes=="${notes=//$'\r'/'%0D'}"

echo "::set-output name=value::$notes"
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name: Rust
name: Build & Test

on: [push]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Build
run: cargo build --verbose

- name: Run tests
run: cargo test --verbose
52 changes: 22 additions & 30 deletions .github/workflows/create_release.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
name: Create Release
name: "Create Release"

on:
push:
# Sequence of patterns matched against refs/tags
tags:
- 'v*' # e.g. v1.0, v20.15.10
# Matches anything starting with a semantic version
- "v[0-9]+.[0-9]+.[0-9]+**"

jobs:
build:
name: Create Release
runs-on: ubuntu-latest
name: "Create Release"
runs-on: "ubuntu-latest"
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install markdown-extract
run: cargo install markdown-extract
- name: "Checkout code"
uses: "actions/checkout@v2"

- name: Get tag string
id: tag
run: echo ::set-output name=value::${GITHUB_REF/refs\/tags\//}
- name: "Construct tag string"
id: "tag"
run: "echo ::set-output name=value::${GITHUB_REF/refs\/tags\//}"

- name: Get release body
id: release_notes
run: |
text=`~/.cargo/bin/markdown-extract -i ${{ steps.tag.outputs.value }} CHANGELOG.md`
echo $text
text="${text//'%'/'%25'}"
text="${text//$'\n'/'%0A'}"
text="${text//$'\r'/'%0D'}"
echo "::set-output name=value::$text"
- name: Create release
id: create_release
uses: actions/create-release@master
- name: "Extract release notes"
id: "notes"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RELEASE_NAME: ${{ steps.tags.outputs.value }}
CHANGELOG_PATH: "./CHANGELOG.md"
run: "./.github/scripts/extract_notes.sh"
shell: "bash"

- name: "Create release"
uses: "softprops/action-gh-release@v1"
with:
name: "Release ${{ steps.tags.outputs.value }}"
tag_name: ${{ github.ref }}
release_name: Release ${{ steps.tags.outputs.value }}
body: |
${{ steps.release_notes.outputs.value }}
body: ${{ steps.notes.outputs.value }}
token: ${{ secrets.GITHUB_TOKEN }}
draft: true
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
# Changelog

Patch notes are automatically extracted from this changelog whenever a tag is
pushed to the GitHub repository. The tag name must match a heading exactly.
pushed to the GitHub repository. The heading must start with the tag name.

## v2.0.0 (January 2021)

In this release, `markdown-extract` has been dramatically simplified, and comes
with a more sensible API out of the box. There are a number of breaking changes.

- Fixed matching headings inside code blocks.
- Remove `--regex` flag. All inputs will be treated as regular expressions.
- Remove `--first` flag. This is now the default behaviour.
- Add `--all` flag. When setting this flag, all matches will be printed (not
just the first).
- Renamed `--ignore-first-heading` to `--no-print-matched-heading`. Behaviour is
the same.
- Clarified help text for the `--no-print-matched-heading` flag.

## v1.1.0

Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "markdown-extract"
description = "Extract sections of a markdown file."
version = "1.1.0"
version = "2.0.0"
authors = ["Sean Bailey <[email protected]>"]
license = "MIT"
repository = "https://github.com/sean0x42/markdown-extract"
Expand All @@ -10,11 +10,13 @@ edition = "2018"
exclude = [".github/*"]
readme = "README.md"

[lib]
path = "src/lib.rs"

[[bin]]
name = "markdown-extract"
path = "src/bin.rs"

[dependencies]
log = "0.4"
regex = "1.3"
structopt = "0.3"
31 changes: 25 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
FROM rust:1.43 as builder
# Dockerfile for creating statically-linked Rust applications.
# See: https://www.artificialworlds.net/blog/2020/04/22/creating-a-tiny-docker-image-of-a-rust-project/
# See: https://alexbrand.dev/post/how-to-package-rust-applications-into-minimal-docker-containers/

# 1: Build the exe
FROM rust:1.57 as builder
WORKDIR /usr/src

# 1a: Prepare for static linking
RUN apt-get update && \
apt-get dist-upgrade -y && \
apt-get install -y musl-tools && \
rustup target add x86_64-unknown-linux-musl

# 1b: Download and compile Rust dependencies (and store as a separate Docker layer)
RUN USER=root cargo new markdown-extract
WORKDIR /usr/src/markdown-extract
COPY . .
RUN cargo install --path .
COPY Cargo.toml Cargo.lock ./
COPY src ./src
RUN cargo install --target x86_64-unknown-linux-musl --path .

FROM debian:buster-slim
COPY --from=builder /usr/local/cargo/bin/markdown-extract /usr/local/bin/markdown-extract
ENTRYPOINT ["markdown-extract"]
# 2: Copy the exe and extra files ("static") to an empty Docker image
FROM scratch
COPY --from=builder /usr/local/cargo/bin/markdown-extract .
# COPY static .
USER 1000
ENTRYPOINT ["./markdown-extract"]
86 changes: 45 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
<div align="center">

# Markdown Extract

Extract sections of a markdown file. This project mostly exists to help me learn
Rust, and to fill a niche requirement for extracting patch notes from
a `CHANGELOG.md`.
[![Crates.io](https://img.shields.io/crates/v/markdown-extract)](https://crates.io/crates/markdown-extract)
[![Docker Pulls](https://img.shields.io/docker/pulls/sean0x42/markdown-extract)](https://hub.docker.com/r/sean0x42/markdown-extract)
[![Build & Test](https://github.com/sean0x42/markdown-extract/actions/workflows/build_and_test.yml/badge.svg)](https://github.com/sean0x42/markdown-extract/actions/workflows/build_and_test.yml)

## Use Cases
</div>

There aren't many, to be honest.
Extract sections of a markdown file with a regular expression! Great for changelogs ;)

1. I created this tool to extract patch notes from a `CHANGELOG.md` by version.
2. The talented folks at HashiCorp are using `markdown-extract` to extract API
documentation, and inject it into OpenAPI schemas.
## Usage

If you have another use for this tool, please let me know!
Given a document called `my-document.md`:

```markdown
# Welcome

This is my amazing markdown document.

## Extract me!

This section should be pulled out.
```

You can extract the second section with the following command:

```console
$ markdown-extract "Extract me!" my-document.md
## Extract me!

This section should be pulled out.
```

## Installation

If you've got Rust installed on your system, you can simple install
If you've got Rust installed on your system, you can simply install
`markdown-extract` with Cargo.

```console
Expand All @@ -25,49 +44,34 @@ $ cargo install markdown-extract

### Docker

A Docker container is also available, and can be installed with the following
A Docker image is also available, and can be installed with the following
command:

```console
$ docker pull sean0x42/markdown-extract
$ docker pull sean0x42/markdown-extract:v2
```

You can then run the container with the following command:

```console
$ docker run -it sean0x42/markdown-extract --help
$ docker run -it sean0x42/markdown-extract:v2 --help
```

## Usage

View the help guide if you like.
Note that because markdown-extract accesses the file system, you will need
to mount a volume if you want to access a file on the host. e.g.

```console
$ markdown-extract --help
markdown-extract 1.1.0
Extract sections of a markdown file

USAGE:
markdown-extract [FLAGS] <pattern> <path>

FLAGS:
-s, --case-sensitive Treat pattern as case sensitive
-f, --first Only return the first match
-h, --help Prints help information
-i, --ignore-first-heading Do not include the top level section heading
-r, --regex Compile pattern as a regular expression
-V, --version Prints version information

ARGS:
<pattern> Pattern to match against section headings
<path> Path to markdown file
``` console
$ docker run -v $PWD:/opt -it sean0x42/markdown-extract:v2 v2.0.0 /opt/CHANGELOG.md
```

Then extract matching sections in a markdown file.
If you know a better way of achieving this, please let me know!

```console
$ markdown-extract --fr "^v1" CHANGELOG.md
## v1.1.0
## Use Cases

...
```
There aren't many, to be honest.

1. Extract patch notes from a `CHANGELOG.md` by version.
2. The talented folks at HashiCorp are using `markdown-extract` to extract API
documentation, and inject it into OpenAPI schemas.

If you have another use for this tool, please let me know!
Loading

0 comments on commit e923cc4

Please sign in to comment.