Skip to content

Commit

Permalink
add pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
devidw committed Aug 23, 2022
1 parent 9b2e62e commit e958705
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 76 deletions.
21 changes: 21 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.245.0/containers/python-3/.devcontainer/base.Dockerfile

# [Choice] Python version (use -bullseye variants on local arm64/Apple Silicon): 3, 3.10, 3.9, 3.8, 3.7, 3.6, 3-bullseye, 3.10-bullseye, 3.9-bullseye, 3.8-bullseye, 3.7-bullseye, 3.6-bullseye, 3-buster, 3.10-buster, 3.9-buster, 3.8-buster, 3.7-buster, 3.6-buster
ARG VARIANT="3.10-bullseye"
FROM mcr.microsoft.com/vscode/devcontainers/python:0-${VARIANT}

# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
ARG NODE_VERSION="none"
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi

# [Optional] If your pip requirements rarely change, uncomment this section to add them to the image.
# COPY requirements.txt /tmp/pip-tmp/
# RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \
# && rm -rf /tmp/pip-tmp

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>

# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
53 changes: 53 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.245.0/containers/python-3
{
"name": "Python 3",
"build": {
"dockerfile": "Dockerfile",
"context": "..",
"args": {
// Update 'VARIANT' to pick a Python version: 3, 3.10, 3.9, 3.8, 3.7, 3.6
// Append -bullseye or -buster to pin to an OS version.
// Use -bullseye variants on local on arm64/Apple Silicon.
"VARIANT": "3.10-bullseye",
// Options
"NODE_VERSION": "none"
}
},
// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"python.defaultInterpreterPath": "/usr/local/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
"python.linting.banditPath": "/usr/local/py-utils/bin/bandit",
"python.linting.flake8Path": "/usr/local/py-utils/bin/flake8",
"python.linting.mypyPath": "/usr/local/py-utils/bin/mypy",
"python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle",
"python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
"python.linting.pylintPath": "/usr/local/py-utils/bin/pylint"
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance",
"GitHub.copilot-labs",
"GitHub.copilot",
"bungcip.better-toml",
"tamasfe.even-better-toml"
]
}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "pip3 install --user -r requirements.txt",
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
65 changes: 0 additions & 65 deletions README.adoc

This file was deleted.

59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<h1 align=center>
<img src=https://raw.githubusercontent.com/devidw/obsidian-to-hugo/master/img/gopher-obsidian.png width=100 height=100>
<br>
Obsidian Vault to Hugo Content
</h1>

Lightweight, zero-dependency CLI written in Python to help us publish [obsidian](https://obsidian.md) notes with [hugo](https://gohugo.io).

It only takes two arguments: The obsidian vault directory (`--obsidian-vault-dir`) and the hugo content directory (`--hugo-content-dir`).

```console
python -m obsidian_to_hugo --obsidian-vault-dir=<path> --hugo-content-dir=<path>
```

It takes care of the following steps:

- Clears hugo content directory (directory will be deleted and recreated)
- Copies obsidian vault contents into hugo content directory (`.obsidian` gets removed immediately after copying)
- Replaces obsidian wiki links (`[[wikilink]]`) with hugo shortcode links (`[wikilink]({{< ref "wikilink" >}})`)


## Replacement examples

| Obsidian | Hugo
| -------- | --------
| ![](https://raw.githubusercontent.com/devidw/obsidian-to-hugo/master/img/obsidian.png) | ![](https://raw.githubusercontent.com/devidw/obsidian-to-hugo/master/img/hugo.png)
| `[[/some/wiki/link]]` | `[/some/wiki/link]({{< ref "/some/wiki/link" >}})`
| `[[/some/wiki/link\|Some text]]` | `[Some text]({{< ref "/some/wiki/link" >}})`
| `[[/some/wiki/link/_index]]` | `[/some/wiki/link/]({{< ref "/some/wiki/link/" >}})`

> NOTE: For now, there is *no way to escape* obsidian wiki links. Every link
> will be replaced with a hugo link. The only way to get around this is changing
> the wiki link to don't match the exact sytax, for example by adding an
> [invisible space](https://en.wikipedia.org/wiki/Zero-width_space) (Obsidian will highlight the invisible character as a red dot).
> ![](https://raw.githubusercontent.com/devidw/obsidian-to-hugo/master/img/do-not-do-that.png)
> However, this still is really really *not* best
> practice, so if anyone wants to implement real escaping, [please do
> so](https://github.com/devidw/obsidian-to-hugo/pulls).

## Installation

```console
pip install obsidian_to_hugo
```


## Usage

```console
usage: __main__.py [-h] [--hugo-content-dir HUGO_CONTENT_DIR] [--obsidian-vault-dir OBSIDIAN_VAULT_DIR]

options:
-h, --help show this help message and exit
--hugo-content-dir HUGO_CONTENT_DIR
Directory of your Hugo content directory, the obsidian notes should be processed into.
--obsidian-vault-dir OBSIDIAN_VAULT_DIR
Directory of the Obsidian vault, the notes should be processed from.
```
Binary file modified img/gopher-obsidian.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "obsidian_to_hugo"
version = "0.1.0"
description = "Process Obsidian notes to publish them with Hugo. Transforms Obsidian wiki links into Hugo shortcodes for internal linking."
readme = "README.md"
requires-python = ">=3.6"
license = { text = "MIT" }
keywords = ["obsidian", "hugo", "markdown", "wikilink"]
authors = [{ name = "David Wolf", email = "[email protected]" }]
classifiers = [
"Typing :: Typed",
"Environment :: Console",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Topic :: Text Processing :: Markup",
]

[project.urls]
"Source" = "https://github.com/devidw/obsidian-to-hugo"
"Bug Tracker" = "https://github.com/devidw/obsidian-to-hugo/issues"
File renamed without changes.
15 changes: 10 additions & 5 deletions src/__main__.py → src/obsidian_to_hugo/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""
Obsidian to Hugo CLI
"""

import argparse
import os
from obsidian_to_hugo import ObsidianToHugo
from .obsidian_to_hugo import ObsidianToHugo

__version__ = "0.1.0"

Expand All @@ -18,10 +22,11 @@
type=str,
)

args = parser.parse_args()


def main(parser: argparse.ArgumentParser) -> None:
def main() -> None:
"""
Main entry point of the CLI.
"""
args = parser.parse_args()
if not args.hugo_content_dir or not os.path.isdir(args.hugo_content_dir):
parser.error("The hugo content directory does not exist.")
Expand All @@ -35,4 +40,4 @@ def main(parser: argparse.ArgumentParser) -> None:


if __name__ == "__main__":
main(parser)
main()
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import os
import shutil
from distutils.dir_util import copy_tree
from wiki_links_processor import replace_wiki_links_helper
from .wiki_links_processor import replace_wiki_links_helper


class ObsidianToHugo:
"""
Process the obsidian vault and convert it to hugo ready content.
"""

def __init__(
self,
obsidian_vault_dir: str,
Expand All @@ -19,7 +23,9 @@ def __init__(

def process(self) -> None:
"""
Delete the hugo content directory and copy the obsidian vault to the hugo content directory, then process the content so that the wiki links are replaced with the hugo links.
Delete the hugo content directory and copy the obsidian vault to the
hugo content directory, then process the content so that the wiki links
are replaced with the hugo links.
"""
self.clear_hugo_content_dir()
self.copy_obsidian_vault_to_hugo_content_dir()
Expand All @@ -44,13 +50,14 @@ def copy_obsidian_vault_to_hugo_content_dir(self) -> None:

def process_content(self) -> None:
"""
Looping through all files in the hugo content directory and replace the wiki links of each matching file.
Looping through all files in the hugo content directory and replace the
wiki links of each matching file.
"""
for root, dirs, files in os.walk(self.hugo_content_dir):
for file in files:
if file.endswith(".md"):
with open(os.path.join(root, file), "r") as f:
with open(os.path.join(root, file), "r", encoding="utf-8") as f:
text = f.read()
text = replace_wiki_links_helper(text)
with open(os.path.join(root, file), "w") as f:
with open(os.path.join(root, file), "w", encoding="utf-8") as f:
f.write(text)
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
import wiki_links_processor
from obsidian_to_hugo import wiki_links_processor


class TestWikiLinksProcessor(unittest.TestCase):
Expand Down

0 comments on commit e958705

Please sign in to comment.