Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added flag --title-from-filename to always use the filename as the page title #115

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ The title of the page can be sourced from multiple places, in the following orde
* The first top-level header found in the document (i.e., the first `#` header)
* The filename, if there are no top-level headers.

You can also use the `--title-from-filename` option to overwrite this behaviour.
This will always use the filename as the page title, even if there are top-level headers or a title entry in the front matter of the document.

Note that if you are reading from standard input, you must either specify the title through the command line or include a title as a header or in the front matter within the content.

To avoid repeating the top level header in the body of the page, pass the `--strip-top-header` parameter to strip it from the document.
Expand Down
17 changes: 16 additions & 1 deletion md2cf/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,17 @@ def get_parser():
help="upload the page tree starting from the top level (no top level parent)",
)

page_group.add_argument(
title_group = page_group.add_mutually_exclusive_group()
title_group.add_argument(
"-t",
"--title",
help="a title for the page. Determined from the document if missing",
)
title_group.add_argument(
"--title-from-filename",
action="store_true",
help="always use the filename as the title of the page",
)

page_group.add_argument(
"-c",
Expand Down Expand Up @@ -681,6 +687,13 @@ def collect_pages_to_upload(args):
)
sys.exit(1)

if args.title_from_filename:
error_console.log(
"You cannot specify --title-from-filename "
"if uploading from standard input\n"
)
sys.exit(1)

if args.title:
pages_to_upload[0].title = args.title
else:
Expand All @@ -697,6 +710,7 @@ def collect_pages_to_upload(args):
use_pages_file=args.use_pages_file,
use_gitignore=args.use_gitignore,
enable_relative_links=args.enable_relative_links,
page_title_from_filename=args.title_from_filename,
)
else:
try:
Expand All @@ -709,6 +723,7 @@ def collect_pages_to_upload(args):
strip_header=args.strip_top_header,
remove_text_newlines=args.remove_text_newlines,
enable_relative_links=enable_relative_links,
page_title_from_filename=args.title_from_filename,
)
)
except FileNotFoundError:
Expand Down
6 changes: 5 additions & 1 deletion md2cf/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def get_pages_from_directory(
remove_text_newlines: bool = False,
use_gitignore: bool = True,
enable_relative_links: bool = False,
page_title_from_filename: bool = False,
) -> List[Page]:
"""
Collect a list of markdown files recursively under the file_path directory.
Expand All @@ -107,6 +108,7 @@ def get_pages_from_directory(
search
:param enable_relative_links: extract all relative links and replace them with
placeholders
:param page_title_from_filename: use the filename as the page title
:return: A list of paths to the markdown files to upload.
"""
processed_pages = list()
Expand Down Expand Up @@ -193,6 +195,7 @@ def get_pages_from_directory(
strip_header=strip_header,
remove_text_newlines=remove_text_newlines,
enable_relative_links=enable_relative_links,
page_title_from_filename=page_title_from_filename,
)
processed_page.parent_title = parent_page_title
processed_pages.append(processed_page)
Expand All @@ -211,6 +214,7 @@ def get_page_data_from_file_path(
strip_header: bool = False,
remove_text_newlines: bool = False,
enable_relative_links: bool = False,
page_title_from_filename: bool = False,
) -> Page:
if not isinstance(file_path, Path):
file_path = Path(file_path)
Expand All @@ -231,7 +235,7 @@ def get_page_data_from_file_path(
enable_relative_links=enable_relative_links,
)

if not page.title:
if page_title_from_filename or not page.title:
page.title = file_path.stem

page.file_path = file_path
Expand Down
22 changes: 22 additions & 0 deletions test_package/unit/test_argument_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest

from md2cf.__main__ import get_parser


def test_specify_both_title_and_title_from_filename_does_exit():
parser = get_parser()

with pytest.raises(SystemExit):
parser.parse_args(["--title", "some-title", "--title-from-filename"])


def test_specify_only_title_does_not_exit():
parser = get_parser()

parser.parse_args(["--title", "some-title"])


def test_specify_only_title_from_filename_does_not_exit():
parser = get_parser()

parser.parse_args(["--title-from-filename"])
48 changes: 48 additions & 0 deletions test_package/unit/test_document_title.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from pathlib import Path

import md2cf.document as doc
from test_package.utils import FakePage


def test_get_page_title_from_file(fs):
fs.create_file("/root-folder/some-page.md", contents="# Title from within file")

result = doc.get_page_data_from_file_path(
Path("/root-folder/some-page.md"), page_title_from_filename=False
)
assert result == FakePage(
title="Title from within file",
)


def test_get_page_title_from_filename_if_no_title_in_file(fs):
fs.create_file("/root-folder/some-page.md")

result = doc.get_page_data_from_file_path(
Path("/root-folder/some-page.md"), page_title_from_filename=False
)
assert result == FakePage(
title="some-page",
)


def test_get_page_title_from_filename_if_no_page_title_in_file(fs):
fs.create_file("/root-folder/some-page.md", contents="")

result = doc.get_page_data_from_file_path(
Path("/root-folder/some-page.md"), page_title_from_filename=True
)
assert result == FakePage(
title="some-page",
)


def test_get_page_title_from_filename_if_page_title_in_file(fs):
fs.create_file("/root-folder/some-page.md", contents="# Title from within file")

result = doc.get_page_data_from_file_path(
Path("/root-folder/some-page.md"), page_title_from_filename=True
)
assert result == FakePage(
title="some-page",
)