Skip to content

Commit

Permalink
Change custom page files to use a .md extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
zedseven committed May 2, 2023
1 parent 94d56c0 commit 3cf0e51
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
12 changes: 6 additions & 6 deletions docs/src/usage_custom_pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ file](config_directories.html).

To document internal command line tools, or if you want to replace an existing
tldr page with one that's better suited for you, place a file with the name
`<command>.page` in the custom pages directory. When calling `tldr <command>`,
`<command>.page.md` in the custom pages directory. When calling `tldr <command>`,
your custom page will be shown instead of the upstream version in the cache.

Path:

$CUSTOM_PAGES_DIR/<command>.page
$CUSTOM_PAGES_DIR/<command>.page.md

Example:

~/.local/share/tealdeer/pages/ufw.page
~/.local/share/tealdeer/pages/ufw.page.md

## Custom Patches

Sometimes you don't want to fully replace an existing upstream page, but just
want to extend it with your own examples that you frequently need. In this
case, use a file called `<command>.patch`, it will be appended to existing
case, use a file called `<command>.patch.md`, it will be appended to existing
pages.

Path:

$CUSTOM_PAGES_DIR/<command>.patch
$CUSTOM_PAGES_DIR/<command>.patch.md

Example:

~/.local/share/tealdeer/pages/ufw.patch
~/.local/share/tealdeer/pages/ufw.patch.md
14 changes: 7 additions & 7 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl Cache {
.find(|path| path.exists() && path.is_file())
}

/// Look up custom patch (<name>.patch). If it exists, store it in a variable.
/// Look up custom patch (<name>.patch.md). If it exists, store it in a variable.
fn find_patch(patch_name: &str, custom_pages_dir: Option<&Path>) -> Option<PathBuf> {
custom_pages_dir
.map(|custom_dir| custom_dir.join(patch_name))
Expand All @@ -249,8 +249,8 @@ impl Cache {
custom_pages_dir: Option<&Path>,
) -> Option<PageLookupResult> {
let page_filename = format!("{name}.md");
let patch_filename = format!("{name}.patch");
let custom_filename = format!("{name}.page");
let patch_filename = format!("{name}.patch.md");
let custom_filename = format!("{name}.page.md");

// Determine directory paths
let pages_dir = self.pages_dir();
Expand All @@ -265,7 +265,7 @@ impl Cache {
})
.collect();

// Look up custom page (<name>.page). If it exists, return it directly
// Look up custom page (<name>.page.md). If it exists, return it directly
if let Some(config_dir) = custom_pages_dir {
let custom_page = config_dir.join(custom_filename);
if custom_page.exists() && custom_page.is_file() {
Expand Down Expand Up @@ -403,8 +403,8 @@ mod tests {
fn test_reader_with_patch() {
// Write test files
let dir = tempfile::tempdir().unwrap();
let page_path = dir.path().join("test.page");
let patch_path = dir.path().join("test.patch");
let page_path = dir.path().join("test.page.md");
let patch_path = dir.path().join("test.patch.md");
{
let mut f1 = File::create(&page_path).unwrap();
f1.write_all(b"Hello\n").unwrap();
Expand All @@ -427,7 +427,7 @@ mod tests {
fn test_reader_without_patch() {
// Write test file
let dir = tempfile::tempdir().unwrap();
let page_path = dir.path().join("test.page");
let page_path = dir.path().join("test.page.md");
{
let mut f = File::create(&page_path).unwrap();
f.write_all(b"Hello\n").unwrap();
Expand Down
File renamed without changes.
20 changes: 10 additions & 10 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ impl TestEnv {
fn add_page_entry(&self, name: &str, contents: &str) {
let dir = self.custom_pages_dir.path();
create_dir_all(dir).unwrap();
let mut file = File::create(dir.join(format!("{name}.page"))).unwrap();
let mut file = File::create(dir.join(format!("{name}.page.md"))).unwrap();
file.write_all(contents.as_bytes()).unwrap();
}

/// Add custom patch entry to the custom_pages_dir
fn add_patch_entry(&self, name: &str, contents: &str) {
let dir = self.custom_pages_dir.path();
create_dir_all(dir).unwrap();
let mut file = File::create(dir.join(format!("{name}.patch"))).unwrap();
let mut file = File::create(dir.join(format!("{name}.patch.md"))).unwrap();
file.write_all(contents.as_bytes()).unwrap();
}

Expand Down Expand Up @@ -660,7 +660,7 @@ fn test_autoupdate_cache() {
check_cache_updated(false);
}

/// End-end test to ensure .page files overwrite pages in cache_dir
/// End-end test to ensure .page.md files overwrite pages in cache_dir
#[test]
fn test_custom_page_overwrites() {
let testenv = TestEnv::new();
Expand All @@ -673,7 +673,7 @@ fn test_custom_page_overwrites() {

// Add file that should be ignored to the cache dir
testenv.add_entry("inkscape-v2", "");
// Add .page file to custome_pages_dir
// Add .page.md file to custom_pages_dir
testenv.add_page_entry("inkscape-v2", include_str!("inkscape-v2.md"));

// Load expected output
Expand All @@ -687,7 +687,7 @@ fn test_custom_page_overwrites() {
.stdout(diff(expected));
}

/// End-End test to ensure that .patch files are appended to pages in the cache_dir
/// End-End test to ensure that .patch.md files are appended to pages in the cache_dir
#[test]
fn test_custom_patch_appends_to_common() {
let testenv = TestEnv::new();
Expand All @@ -700,8 +700,8 @@ fn test_custom_patch_appends_to_common() {

// Add page to the cache dir
testenv.add_entry("inkscape-v2", include_str!("inkscape-v2.md"));
// Add .page file to custome_pages_dir
testenv.add_patch_entry("inkscape-v2", include_str!("inkscape-v2.patch"));
// Add .page.md file to custom_pages_dir
testenv.add_patch_entry("inkscape-v2", include_str!("inkscape-v2.patch.md"));

// Load expected output
let expected = include_str!("inkscape-patched-no-color.expected");
Expand All @@ -714,7 +714,7 @@ fn test_custom_patch_appends_to_common() {
.stdout(diff(expected));
}

/// End-End test to ensure that .patch files are not appended to .page files in the custom_pages_dir
/// End-End test to ensure that .patch.md files are not appended to .page.md files in the custom_pages_dir
/// Maybe this interaction should change but I put this test here for the coverage
#[test]
fn test_custom_patch_does_not_append_to_custom() {
Expand All @@ -730,8 +730,8 @@ fn test_custom_patch_does_not_append_to_custom() {

// Add page to the cache dir
testenv.add_page_entry("inkscape-v2", include_str!("inkscape-v2.md"));
// Add .page file to custome_pages_dir
testenv.add_patch_entry("inkscape-v2", include_str!("inkscape-v2.patch"));
// Add .page.md file to custom_pages_dir
testenv.add_patch_entry("inkscape-v2", include_str!("inkscape-v2.patch.md"));

// Load expected output
let expected = include_str!("inkscape-default-no-color.expected");
Expand Down

0 comments on commit 3cf0e51

Please sign in to comment.