Skip to content

Remove line breaks custom tool

jonsnow231 edited this page Apr 18, 2024 · 4 revisions

A Great Example To Show The Usage Of The "Custom Tools"

There are three different tools listed. The first two were tested on Windows portable install. The first one is used for highlighting text and the custom tool modifies that text to remove line breaks and adding a space (commonly for pdf). The second does the same thing but modifies whatever is in your clipboard and pastes it (to save time). The third modifies the entire document.

For more information on the first two, see this thread: https://github.com/zim-desktop-wiki/zim-desktop-wiki/discussions/2389#discussioncomment-6242995

To help improve the first one, you can figure out how to apply them with the Python interpreter already in the portable Zim install so installing Python is not necessary. Or even better, create a plugin for the second one so that you can just press Ctrl+Shift+V to paste in the modified contents.


There are two options. The first is to change highlighted text to remove line breaks. The second is to paste whatever is in your clipboard modified to remove the line breaks.

HIGHLIGHT REMOVE LINE BREAKS

  1. Download the newest Python 3 if you don't have it already: https://www.python.org/downloads/
  2. Open a text document and paste the following code:
import sys

args = ' '.join(sys.argv[1:]).replace('\n',' ')

formatted = args.replace('  ','\n') 

print(formatted)
  1. Save it as removelinebreaks.py anywhere in your ZIM folder. I chose Zim\share\zim\custom tools (I created the custom tools folder)
  2. Open Zim Wiki and go to Tools > Custom Tools. A pop up will appear where you will click the "+" button.
  3. Input the following: Name: Remove Line Breaks Description: Removes line breaks on highlight Command: "C:\Program Files\Python311\python.exe" "C:\Users\USERNAME\Documents\PORTABLE apps\Zim\share\zim\custom tools\removelinebreaks.py" %T Icon: leave or click it to change to whatever you want Command line does not modify data: unchecked Output should replace current selection: checked Show in toolbar: checked (optional)

(Under command, the first part is wherever your python.exe was installed onto in step one. The second is wherever you saved your .py python script.

image

  1. Finished. Now highlight text, and click the custom tool button (or go to Tools > Remove Line Breaks).

PASTE REMOVE LINE BREAKS

  1. Exactly the same as above, but also install Pyperclip module for Python: https://pypi.org/project/pyperclip/ For windows: open CMD in administrator mode, and type: pip install pyperclip3.
  2. Open a text document and paste the following code:
import sys
import pyperclip

args = pyperclip.paste().splitlines()
args = ' '.join(args) 

formatted = args.replace('  ','\n')

print(formatted)
  1. Save it as pasteremoveline.py anywhere in your ZIM folder. I chose Zim\share\zim\custom tools (I created the custom tools folder)

Everything else is the same as the highlight remove line breaks, but using this python script instead of the other.

  1. Now when something is in your clipboard (you copied from a pdf or elsewhere), click the custom tool button (or Tools > Paste Line Break).

Assign a Key Binding to this Custom Tool (Ctrl+Shift+V)

  1. Open the Preferences window (EditPreferences on the menu bar)
  2. Go to the Key bindings tab
  3. There is an action for the Custom Tool you just created. It's probably the 2nd or 3rd one in the list and the name might look like
    <Actions>/custom_tools/paste removed lines-usercreated
  4. Double click the Key Binding cell (it probably says "Disabled")
  5. Press Ctrl+Shift+V
  6. Click OK

Created by nomnex

Case: How to remove the manual line breaks of simple text emails (e.g. Newsgroups, private msg., etc.) you past in Zim.

  • Download and put the script in your "/home/user/bin" directory or wherever you like
  • Make it executable.
  • Go into Zim Tools --> Custom Tools, and add the script with the "%f" commandline argument.

This will add the script in the tools menu and you can process pages in one go.

file "remove_line_breaks.py":

#!/usr/bin/python

import sys
import re


def split_headers(text):
	'''Split zim headers from text and removes both seperately'''
	if text.startswith('Content-Type:'):
		# mail style headers
		headers, text = text.split('\n\n', 1)
			# split on first empty line
		return headers, text
	else: 
		# no zim headers
		return '', text


def join_headers(headers, text):
	'''Join zim headers with body text, returns single page source'''
	if headers:
		return headers.rstrip() + '\n\n' + text.lstrip()
	else:
		return text.lstrip()


def remove_line_breaks(text):
	'''Removes line breaks within paragraphs, but keeps empty lines'''
	pattern = re.compile(r'^[ \t]+\n', re.M) # pattern for empty lines
	text = pattern.sub('\n', text) # fix empty lines to be really empty

	pattern = re.compile(r'(?<!==)\n') # pattern for newline not at end of heading in zim wiki syntax
	parts = text.split('\n\n') # split on empty lines
	parts = [pattern.sub(' ', p) for p in parts] # replace line breaks with space
	parts = [p for p in parts if len(p) and not p.isspace()] # remove empty para
	return '\n\n'.join(parts) # join with empty lines


def remove_line_breaks_in_zim_page(file):
	'''Remove line breaks in a zim page file'''
	fh = open(file)
	text = fh.read()
	fh.close()

	headers, text = split_headers(text)
	text = remove_line_breaks(text)
	text = join_headers(headers, text)

	fh = open(file, 'w')
	fh.write(text)
	fh.close()


if __name__ == '__main__':
	file = sys.argv[1] # first commandline argument
	remove_line_breaks_in_zim_page(file)
Clone this wiki locally