-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit eadba0b
Showing
6 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Copyright 2020 CoolSpring8 | ||
|
||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# AfterCopy | ||
|
||
有时候需要从 PDF、CAJ 等文件的阅读器窗口中复制出大段文字,但是这样得到的文字粘贴到 Word 里往往不尽人意:原排版的每一行都会被“另起一段”,有些中文标点变成了带有空格的英文标点……手动调整比较烦琐,因此我写了一个简单的脚本来处理这件事。 | ||
|
||
## Quick Start | ||
|
||
``` | ||
pip install aftercopy | ||
aftercopy -v | ||
``` | ||
|
||
然后去阅读器中复制文字,再粘贴时得到的已经是处理好(去掉换行、替换标点)的结果了。 | ||
|
||
使用完毕后请记得关闭,避免影响常规的复制粘贴用途。 | ||
|
||
## 用法 | ||
|
||
``` | ||
aftercopy --help | ||
Usage: aftercopy [OPTIONS] | ||
Options: | ||
-p, --passive Disable active reading from clipboard. Instead you can | ||
paste into and copy from terminal. End your input with | ||
Ctrl-Z + Enter (Windows) or Ctrl-D + Enter. | ||
-v, --verbose Display the concrete re-copied text and more info. | ||
-l, --lang [cn|en] Switch type of language in text. This will influence the | ||
rule set used. (Chinese by default) | ||
--help Show this message and exit. | ||
``` | ||
|
||
## 原理 | ||
|
||
每隔 2 秒读一次剪贴板,若发生改变则对新读入的文字作相应的处理,将结果重新写入剪贴板。 | ||
|
||
## TODO | ||
|
||
- 替换规则。目前对于标点的替换规则是硬编码的,显然这种做法大大降低了使用的灵活性。但是我还没有想到在每次运行 / 在安装时指定规则文件的较好办法。 | ||
- 错别字识别。没找到这方面便捷的库。 | ||
|
||
## One more thing... | ||
|
||
请勿用作抄袭等侵犯他人著作权的用途。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import click | ||
import time | ||
import sys | ||
import pyperclip | ||
|
||
|
||
class Text: | ||
def __init__(self, raw, lang): | ||
self.raw = raw | ||
self.lang = lang | ||
self.clean_text() | ||
|
||
def clean_text(self): | ||
rule_set = {"\r": "", "\n": "", "[ ": "[", "] ": "]"} | ||
rule_set_cn = { | ||
"? ": "?", | ||
"! ": "!", | ||
", ": ",", | ||
":": ":", | ||
";": ";", | ||
"(": "(", | ||
")": ")", | ||
"… …": "……", | ||
"`": "‘", | ||
"' ": "’", | ||
} | ||
|
||
self.clean = self.raw | ||
for original, fixed in rule_set.items(): | ||
self.clean = self.clean.replace(original, fixed) | ||
if self.lang == "cn": | ||
for original, fixed in rule_set_cn.items(): | ||
self.clean = self.clean.replace(original, fixed) | ||
|
||
|
||
def copy_echo(text, verbose): | ||
click.echo(time.strftime("%H:%M:%S ", time.localtime()), nl=False) | ||
pyperclip.copy(text) | ||
if verbose: | ||
click.echo(repr(text)) | ||
else: | ||
click.echo("Re-copied!") | ||
|
||
|
||
@click.command() | ||
@click.option( | ||
"-p", | ||
"--passive", | ||
is_flag=True, | ||
help="Disable active reading from clipboard. Instead you can paste into and copy from terminal. End your input with Ctrl-Z + Enter (Windows) or Ctrl-D + Enter.", | ||
) | ||
@click.option( | ||
"-v", | ||
"--verbose", | ||
is_flag=True, | ||
help="Display the concrete re-copied text and more info.", | ||
) | ||
@click.option( | ||
"-l", | ||
"--lang", | ||
type=click.Choice(["cn", "en"]), | ||
default="cn", | ||
help="Switch type of language in text. This will influence the rule set used. (Chinese by default)", | ||
) | ||
def main(passive, verbose, lang): | ||
click.echo("Ready!\n") | ||
|
||
if passive: | ||
# paste and re-copy | ||
s = "" | ||
for line in sys.stdin: | ||
s += line | ||
click.echo(Text(s, lang=lang).clean) | ||
|
||
else: | ||
# read clipboard and re-copy | ||
# initial | ||
tmpText = Text(pyperclip.paste(), lang=lang) | ||
copy_echo(tmpText.clean, verbose=verbose) | ||
time.sleep(2) | ||
|
||
# loop | ||
while True: | ||
newText = Text(pyperclip.paste(), lang=lang) | ||
if newText.raw == tmpText.raw or newText.raw == tmpText.clean: | ||
continue | ||
copy_echo(newText.clean, verbose=verbose) | ||
|
||
tmpText = newText | ||
time.sleep(2) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from setuptools import setup | ||
from os import path | ||
|
||
here = path.abspath(path.dirname(__file__)) | ||
|
||
with open(path.join(here, "README.md"), encoding="utf-8") as f: | ||
long_description = f.read() | ||
|
||
setup( | ||
name="aftercopy", | ||
version="0.1.0", | ||
description="A helper tool that processes text copied from PDF, removing newlines, replacing punctuation and more.", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/coolspring8/aftercopy", | ||
author="CoolSpring8", | ||
author_email="[email protected]", | ||
classifiers=[ | ||
"Development Status :: 3 - Alpha", | ||
"Intended Audience :: End Users/Desktop", | ||
"Topic :: Utilities", | ||
"License :: OSI Approved :: BSD License", | ||
"Programming Language :: Python :: 3", | ||
"Programming Language :: Python :: 3.5", | ||
"Programming Language :: Python :: 3.6", | ||
"Programming Language :: Python :: 3.7", | ||
"Programming Language :: Python :: 3.8", | ||
"Programming Language :: Python :: 3 :: Only", | ||
], | ||
keywords="clipboard text", | ||
py_modules=["aftercopy"], | ||
python_requires=">=3.5, <4", | ||
install_requires=["click>=7.0", "pyperclip>=1.8.0"], | ||
entry_points={"console_scripts": ["aftercopy=aftercopy:main",],}, | ||
project_urls={ | ||
"Bug Reports": "https://github.com/coolspring8/aftercopy/issues", | ||
"Source": "https://github.com/coolspring8/aftercopy/", | ||
}, | ||
) |