Skip to content

Commit

Permalink
fix md_to_gemtext for image links
Browse files Browse the repository at this point in the history
  • Loading branch information
masalachai committed Jul 27, 2021
1 parent bdc2b97 commit eddac6a
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 2 deletions.
14 changes: 14 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ platform:
arch: amd64

steps:
- name: test
image: ayravat/rust:1.53.0-ci
volumes:
- name: ssh
path: /root/.ssh
- name: target
path: /tmp/cargo-target
- name: cache
path: /tmp/cargo
commands:
- export CARGO_HOME=/tmp/cargo
- export CARGO_TARGET_DIR=/tmp/cargo-target
- cargo test --lib -- --nocapture

- name: production
image: ayravat/rust:1.53.0-ci
volumes:
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,6 @@ pub mod actions {
.boxed()
}
}

#[cfg(test)]
pub mod test;
21 changes: 21 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::util::md_to_gemtext;
use std::{fs::File, io::Read};

#[test]
fn test_md_to_gemtext() {
let mut f = File::open("README.md").unwrap();
let mut buf = String::new();

f.read_to_string(&mut buf).unwrap();

let mut gem_f = File::open("tests/resources/README.gmi").unwrap();
let mut gem_buf = String::new();

gem_f.read_to_string(&mut gem_buf).unwrap();

let gemtext = md_to_gemtext(buf.as_str()).unwrap();

println!("{}", gemtext);

assert_eq!(gem_buf, gemtext);
}
6 changes: 4 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ pub fn get_files(dir: &str) -> Result<Vec<String>, Box<dyn Error>> {

pub fn md_to_gemtext(contents: &str) -> Result<String, Box<dyn Error>> {
let link_regex = Regex::new(r"(?x)(?P<text>\[[^\[]*\])\s*(?P<link>\([^\(]*\))")?;
let link_strip = Regex::new(r"[!]*\[[^\[]*\]\s*\([^\(]*\)")?;
let link_strip = Regex::new(r"(\[?!)*\[[^\[]*\]\s*\([^\(]*\)")?;
let replace_link_text_chars = Regex::new(r"\([^\)]*\)")?;
let replace_chars = Regex::new(r"[\[\]\(\)]*")?;
let anchor_link_regex = Regex::new(r"^#.*")?;
let backticks_regex = Regex::new(r"^[`]{3}.*")?;
Expand Down Expand Up @@ -95,7 +96,8 @@ pub fn md_to_gemtext(contents: &str) -> Result<String, Box<dyn Error>> {
}

for link in link_regex.captures_iter(&line) {
let link_text = replace_chars.replace_all(&link["text"], "");
let link_text = replace_link_text_chars.replace_all(&link["text"], "");
let link_text = replace_chars.replace_all(&link_text, "");
let link_uri = replace_chars.replace_all(&link["link"], "");
let mut is_anchor_link = false;

Expand Down
76 changes: 76 additions & 0 deletions tests/resources/README.gmi
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Gemini Git Browser

Build[1]
=> https://ci.ayravat.com/ritesh/gemini-git-browser [1] Build

The gemini git browser is a very basic UI to browse git repositories on the gemini protocol

It can be seen in action at gemini://git.ritesh.ch

Or on https://proxy.vulpes.one/gemini/git.ritesh.ch/ if you're on an HTTP browser.

If the tree contains a README.md file, like github and other popular web UIs for git, it will automatically convert and render the README.md to the gemini page.

Single Repo View[2] File View[3] Repo List View[4] Readme View[5]
=> https://ci.ayravat.com/filestore/single-repo.jpeg [2] Single Repo View
=> https://ci.ayravat.com/filestore/file.jpeg [3] File View
=> https://ci.ayravat.com/filestore/repos.jpeg [4] Repo List View
=> https://ci.ayravat.com/filestore/readme.jpeg [5] Readme View

## Demo

A local demo can be run by cloning the repository and building a local docker container that will host a sample repository within the container.

```
git clone [email protected]:masalachai/gemini-git-browser.git && cd gemini-git-browser
sh deploy/deploy.sh development
docker run -p 1965:1965 -it gemini_git_browser:latest
```

Once the container is running, the capsule can be seen with a gemini browser at gemini://localhost/

## Setup

To use it with your repos, please note the following.

1. Currently, it only works with namespaced repos, i.e. the repo directories must be placed under a parent directory that acts as its namespace. For example masalachai/gemini-git-browser where masalachai is the namespace and gemini-git-browser the repo.

2. A REPO_DIR environment variable needs to be set to the path of these namespace dirs. For example, if your repository is at /repositories/masalachai/gemini-git-browser, REPO_DIR should be set as follows

```
REPO_DIR=/repositories
```

3. A gemini-git-browser.toml file also needs to be created and placed at $XDG_CONFIG_HOME/gemini-git-browser. XDG_CONFIG_HOME is usually set to $HOME/.config on linux. Within the gemini-git-browser.toml there must be a list of namespace dirs and repo dirs which are allowed to be served, and a title, which may be left blank if unneeded.

```
# $HOME/.config/gemini-git-browser/gemini-git-browser.toml
allowed = ["masalachai", "masalachai/gemini-git-browser"]
title = ""
```

Once the REPO_DIR variable and gemini-git-browser.toml file is set, executing the binary should serve the repos at the gemini port.

## Run from Docker

The UI is also packaged as a docker image for easy deployment. To run it:

- create the gemini-git-browser.toml file
- create a TLS certificate and private key (gemini uses TLS by default)
- mount the gemini-git-browser.toml, the certificate, private key, and your repository directory into the container with the following command:

```
docker run -p 1965:1965 \
-v /etc/letsencrypt/live/yourdomain.com/fullchain.pem:/app/cert/cert.pem \
-v /etc/letsencrypt/live/yourdomain.com/privkey.pem:/app/cert/key.pem \
-v /my-repositories:/repositories
-v $HOME/.config/gemini-git-browser/gemini-git-browser.toml:/root/.config/gemini-git-browser/gemini-git-browser.toml
-it ayravat/gemini-git-browser:latest
```
The paths used in this example are the default let's encrypt certificate and key paths and the default location of the gemini-git-browser.toml file. Please adjust the command accordingly if your certificate or toml file paths are different.

Once the container is running you can access the UI at gemini://localhost/

## License

The source is released under the GNU GPL v3, a copy of which is included in the repository

0 comments on commit eddac6a

Please sign in to comment.