Filenames with spaces; output beside the input file; warning fixes (from dev c08eb1bbfa4c)
- Filenames may contain spaces: quote on the command line; filename lists use a standalone "/" separator; unseparated names that do not exist are rejoined into names that do (announced); leading ~ expands. - Without -o, output is written to the input file's directory; -o fully specifies the output directory and basename. - "Word not parsed" warnings now appear without -v, only for text that survives removal, with correct line numbers. - New tst/filename_test.sh regression suite. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -69,3 +69,14 @@ An <id> is the value of the ^:id argument for an image.
|
||||
|
||||
^:pattern before(?^:\s+\d+)?^|after(?^:\s+\d+)?^|[-\w]+
|
||||
@@@
|
||||
|
||||
@@@argtype filename_list |
|
||||
one or more filenames. Filenames may contain spaces: a list is separated
|
||||
by a standalone "/" (whitespace on both sides), e.g.
|
||||
"chapter 1.kt / chapter 2.kt". Without the separator, the names are
|
||||
separated by whitespace, and names that do not exist are rejoined with
|
||||
their neighbors into names that do. A leading ~ expands to the home
|
||||
directory.
|
||||
:pattern [\s\S]*
|
||||
:python_cast (lambda s: __import__("kutil").filename_list(s))
|
||||
@@@
|
||||
|
||||
@@ -219,3 +219,37 @@ def format_for_paragraphs(s):
|
||||
return result
|
||||
|
||||
|
||||
|
||||
def filename_list(text):
|
||||
"""Split a filename list; the Python twin of resolve_filename_list()
|
||||
in mac/file.cpp (keep the two in sync). A standalone "/" (whitespace
|
||||
on both sides) separates names, whose inner spacing is preserved;
|
||||
without a separator, whitespace-separated tokens that do not name
|
||||
existing files are rejoined with their neighbors into names that do.
|
||||
A leading ~ expands to the home directory."""
|
||||
text = text.strip()
|
||||
if not text:
|
||||
return []
|
||||
parts = re.split(r'(?:^|(?<=\s))/(?:\s|$)', text)
|
||||
if len(parts) > 1:
|
||||
return [os.path.expanduser(p.strip()) for p in parts if p.strip()]
|
||||
tokens = text.split()
|
||||
result, i = [], 0
|
||||
while i < len(tokens):
|
||||
if os.path.isfile(os.path.expanduser(tokens[i])):
|
||||
result.append(tokens[i])
|
||||
i += 1
|
||||
continue
|
||||
acc, j, found = tokens[i], i + 1, False
|
||||
while j < len(tokens):
|
||||
acc += " " + tokens[j]
|
||||
j += 1
|
||||
if os.path.isfile(os.path.expanduser(acc)):
|
||||
result.append(acc)
|
||||
i = j
|
||||
found = True
|
||||
break
|
||||
if not found:
|
||||
result.append(tokens[i])
|
||||
i += 1
|
||||
return [os.path.expanduser(p) for p in result]
|
||||
|
||||
Reference in New Issue
Block a user