Skip to content

Commit a879264

Browse files
authored
feat: refactor to use module structure and enhance utilities (#9)
1 parent d9c1c96 commit a879264

File tree

5 files changed

+678
-117
lines changed

5 files changed

+678
-117
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
`github` is an extension for [Quarto](https://quarto.org) to automatically shortens and converts [GitHub references](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/autolinked-references-and-urls) into links.
44

5-
## Installing
5+
## Installation
66

77
```bash
88
quarto add mcanouil/quarto-github
99
```
1010

1111
This will install the extension under the `_extensions` subdirectory.
12+
1213
If you're using version control, you will want to check in this directory.
1314

14-
## Using
15+
## Usage
1516

1617
You can reference GitHub issues, pull requests, and commits in your content using GitHub short references.
1718
This Quarto extension automatically shortens and converts GitHub references into links.
@@ -70,15 +71,15 @@ This is required for references that don't include the repository name (like `#1
7071

7172
Source: [Autolinked references and URLs - GitHub Docs](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/autolinked-references-and-urls).
7273

73-
### Mentioning users
74+
### Mentioning Users
7475

7576
| User/Organisation | Raw reference | Short link |
7677
|----------------------|----------------|---------------|
7778
| User mention | `@mcanouil` | `@mcanouil` |
7879
|----------------------|----------------|---------------|
7980
| Organisation mention | `@quarto-dev` | `@quarto-dev` |
8081

81-
### Issues and pull requests
82+
### Issues and Pull Requests
8283

8384
| Reference type | Raw reference | Short link |
8485
|--------------------------------------------------------------------------------------------|--------------------------------------------------|---------------------------------|
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
--[[
2+
# MIT License
3+
#
4+
# Copyright (c) 2025 Mickaël Canouil
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in all
14+
# copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
# SOFTWARE.
23+
]]
24+
25+
--- MC Git - Git repository utilities for Quarto Lua filters and shortcodes
26+
--- @module git
27+
--- @author Mickaël Canouil
28+
--- @version 1.0.0
29+
30+
local git_module = {}
31+
32+
-- ============================================================================
33+
-- GIT REPOSITORY UTILITIES
34+
-- ============================================================================
35+
36+
--- Check if a string is empty or nil
37+
--- @param s string|nil The string to check
38+
--- @return boolean true if the string is nil or empty
39+
local function is_empty(s)
40+
return s == nil or s == ''
41+
end
42+
43+
--- Get repository name from git remote origin URL
44+
--- Executes shell command to extract repository name from git remote
45+
--- @return string|nil The repository name (e.g., "owner/repo") or nil if not found
46+
--- @usage local repo = git_module.get_repository()
47+
function git_module.get_repository()
48+
local is_windows = package.config:sub(1, 1) == "\\"
49+
local remote_repository_command
50+
51+
if is_windows then
52+
remote_repository_command = "(git remote get-url origin) -replace '.*[:/](.+?)(\\.git)?$', '$1'"
53+
else
54+
remote_repository_command = "git remote get-url origin 2>/dev/null | sed -E 's|.*[:/]([^/]+/[^/.]+)(\\.git)?$|\\1|'"
55+
end
56+
57+
local handle = io.popen(remote_repository_command)
58+
if handle then
59+
local git_repo = handle:read("*a"):gsub("%s+$", "")
60+
handle:close()
61+
if not is_empty(git_repo) then
62+
return git_repo
63+
end
64+
end
65+
66+
return nil
67+
end
68+
69+
-- ============================================================================
70+
-- MODULE EXPORT
71+
-- ============================================================================
72+
73+
return git_module

0 commit comments

Comments
 (0)