Fix: Use working regex patterns with str.replace for reliable query block removal
- Replace pattern.sub() with str.replace() for more reliable matching - Add newline preservation to maintain proper spacing after removal - Clean up trailing whitespace on all lines - Normalize file ending with single trailing newline - Handle all three formats: query-table prefix, collapsed prefix, and standalone This version has been tested and successfully removes all Task List query blocks while preserving non-Task-List queries and maintaining proper file formatting.
This commit is contained in:
parent
a5a1c01553
commit
cc7989067c
1 changed files with 62 additions and 8 deletions
|
|
@ -8,15 +8,69 @@ from typing import Tuple
|
|||
|
||||
|
||||
def remove_task_list_queries(content: str) -> Tuple[str, int]:
|
||||
"""Remove all Task List query blocks from Logseq content."""
|
||||
# Match #+BEGIN_QUERY or +BEGIN_QUERY to #+END_QUERY (or EOF) blocks containing Task/ToDo List title
|
||||
# Handle both proper (# prefix) and malformed (no #) BEGIN_QUERY markers
|
||||
# Make END_QUERY optional to handle truncated files
|
||||
pattern = r'#?\+BEGIN_QUERY[\s\S]*?:title\s+"[^"]*(?:Task|ToDo)[^"]*List[^"]*"' r'[\s\S]*?(?:#?\+END_QUERY|$)'
|
||||
"""Remove all Task List query blocks from Logseq content.
|
||||
|
||||
matches = re.findall(pattern, content)
|
||||
count = len(matches)
|
||||
cleaned = re.sub(pattern, '', content)
|
||||
Handles formats like:
|
||||
- - query-table:: true/false
|
||||
query-sort-by:: page
|
||||
#+BEGIN_QUERY
|
||||
{:title "✅ Task List" ...}
|
||||
#+END_QUERY
|
||||
|
||||
- - collapsed:: true/false
|
||||
#+BEGIN_QUERY
|
||||
{:title "✅ Task List" ...}
|
||||
#+END_QUERY
|
||||
|
||||
- #+BEGIN_QUERY
|
||||
{:title "✅ Task List" ...}
|
||||
#+END_QUERY
|
||||
|
||||
Only removes blocks with "Task" or "ToDo" AND "List" in the title.
|
||||
Other query blocks are preserved.
|
||||
"""
|
||||
count = 0
|
||||
cleaned = content
|
||||
|
||||
# Pattern 1: With "query-table::" prefix
|
||||
p1 = re.compile(
|
||||
r'-\s+query-table::\s+(?:true|false)\s*\n'
|
||||
r'(?:\s+\w+::\s+\S+\s*\n)*'
|
||||
r'(?:\s*#?\+BEGIN_QUERY\s*\n)?'
|
||||
r'[\s\S]*?#?\+END_QUERY\s*\n?',
|
||||
re.MULTILINE
|
||||
)
|
||||
|
||||
# Pattern 2: With "collapsed::" prefix (no query-table)
|
||||
p2 = re.compile(
|
||||
r'-\s+collapsed::\s+(?:true|false)\s*\n'
|
||||
r'(?:\s+\w+::\s+\S+\s*\n)*'
|
||||
r'(?:\s*#?\+BEGIN_QUERY\s*\n)?'
|
||||
r'[\s\S]*?#?\+END_QUERY\s*\n?',
|
||||
re.MULTILINE
|
||||
)
|
||||
|
||||
# Pattern 3: No prefix, just BEGIN_QUERY
|
||||
p3 = re.compile(
|
||||
r'(?:^|\n)\s*#?\+BEGIN_QUERY\s*\n'
|
||||
r'[\s\S]*?#?\+END_QUERY\s*\n?',
|
||||
re.MULTILINE
|
||||
)
|
||||
|
||||
for pattern in [p1, p2, p3]:
|
||||
matches = pattern.findall(cleaned)
|
||||
for match in matches:
|
||||
if ':title' in match and ('Task' in match or 'ToDo' in match) and 'List' in match:
|
||||
# Replace with a single newline to preserve spacing
|
||||
cleaned = cleaned.replace(match, '\n')
|
||||
count += 1
|
||||
|
||||
# Clean up extra blank lines (3+ newlines become 2)
|
||||
cleaned = re.sub(r'\n{3,}', '\n\n', cleaned)
|
||||
# Clean up any leading/trailing whitespace on lines
|
||||
cleaned = '\n'.join(line.rstrip() for line in cleaned.split('\n'))
|
||||
# Remove leading/trailing whitespace from entire content
|
||||
cleaned = cleaned.strip() + '\n'
|
||||
|
||||
return cleaned, count
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue