windingwind / zotero-actions-tags

Customize your Zotero workflow.
GNU Affero General Public License v3.0
1.83k stars 48 forks source link

[Feature]请问可否利用action&tag,实现添加注释以后,自动去除注释内容中的空格以及乱码呢? #265

Closed yzy1228682367 closed 7 months ago

yzy1228682367 commented 7 months ago

Is there an existing issue for this?

Environment

Describe the feature request

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 感谢开发action&tag!自动化省了很多功夫。请问可否利用action&tag实现添加注释以后,自动去除注释内容中的空格以及乱码呢? 291341708430127_ pic

Why do you need this feature? A clear and concise description of why you need this feature. pdf中的中文文本有时空格很多,即使一行内没有空格,换行也会造成空格。目前可以利用快捷指令、quicker等工具选中文本以后去除空格,但是是否可以利用action&tag的功能实现全自动去除空格呢?感谢开发者~ CleanShot 2024-02-20 at 20 12 12@2x

Describe the solution you'd like

The solution you'd like A clear and concise description of what you want to happen.

Alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Anything else?

以下脚本(利用ChatGPT生成的)或许可以提供参考,主要功能是:第一,去除除了英文间的空格外的所有空格;第二,去除所有换行符,除了这个换行符前有“。”;第三,所有全角字符转为半角字符;第四,补全标点符号。

` function run(input, parameters) { // 第一个脚本内容 var inputText1 = String(input[0]);

// 1. 标记换行符
var markedNewLines = inputText1.replace(/\n/g, '<NEWLINE_MARKER>');
markedNewLines = markedNewLines.replace(/\r\n/g, '<CRLF_MARKER>');
markedNewLines = markedNewLines.replace(/\r/g, '<CR_MARKER>');

// 2. 标记英文单词间的空格
var markedEnglishSpaces = markedNewLines.replace(/([a-zA-Z])\s+([a-zA-Z])/g, '$1<ENGLISH_SPACE>$2');

// 3. 标记中文和英文间的空格
var markedChineseEnglishSpaces = markedEnglishSpaces.replace(/([\u4E00-\u9FA5])\s+([a-zA-Z])/g, '$1<CH_EN_SPACE>$2');
markedChineseEnglishSpaces = markedChineseEnglishSpaces.replace(/([a-zA-Z])\s+([\u4E00-\u9FA5])/g, '$1<CH_EN_SPACE>$2');

// 4. 删除其他所有的空格
var withoutSpaces = markedChineseEnglishSpaces.replace(/\s+/g, '');

// 5. 恢复上面的标记为正常的空格
var processedText = withoutSpaces.replace(/<ENGLISH_SPACE>/g, ' ');
processedText = processedText.replace(/<CH_EN_SPACE>/g, ' ');
processedText = processedText.replace(/<NEWLINE_MARKER>/g, '\n');
processedText = processedText.replace(/<CRLF_MARKER>/g, '\r\n');
processedText = processedText.replace(/<CR_MARKER>/g, '\r');

// 6. 替换“。”后的第一个换行符为特殊标记
var intermediateText = processedText.replace(/。\s*([\r\n])/g, '。<SPECIAL_MARKER>');

// 7. 移除其他所有的换行符
var finalText1 = intermediateText.replace(/[\r\n]+/g, '');

// 8. 将特殊标记替换回换行符
finalText1 = finalText1.replace(/<SPECIAL_MARKER>/g, '\n');

// 第二个脚本内容
var inputText2 = finalText1;

// 第一步: 将全角字符转换为半角
var halfWidthText = toHalfWidth(inputText2);

// 第二步: 保留“......”(省略号)不被转换
var ellipsisMarked = halfWidthText.replace(/\.{6}/g, '<ELLIPSIS_MARKER>');

// 第三步: 使用正则表达式替换标点
var replacedPunctuations = ellipsisMarked.replace(/([^\w\sA-Za-z<>])(?![a-zA-Z0-9>])/g, function(match, p1, offset, string) {
    // 判断前后是否是数字或英文,如果是,则直接返回原标点符号
    if ((offset > 0 && /[a-zA-Z0-9]/.test(string[offset - 1])) || (offset + 1 < string.length && /[a-zA-Z0-9]/.test(string[offset + 1]))) {
        return p1;
    }

    switch (p1) {
        case ',':
            return ',';
        case '!':
            return '!';
        case '?':
            return '?';
        case ';':
            return ';';
        case ':':
            return ':';
        case '"':
            // 检查这个双引号是不是右边的
            if (offset > 0 && /[\w\sA-Za-z]/.test(string[offset - 1])) {
                return '”';
            } else {
                return '“';
            }
        case "'":
            // 检查这个单引号是不是右边的
            if (offset > 0 && /[\w\sA-Za-z]/.test(string[offset - 1])) {
                return '’';
            } else {
                return '‘';
            }
        case '(':
            return '(';
        case ')':
            return ')';
        default:
            return p1; // 如果没有对应的中文标点,返回原标点
    }
});

// 第四步: 将省略号从特殊字符恢复
var processedText = replacedPunctuations.replace(/<ELLIPSIS_MARKER>/g, '......');

// 第五步: 删除后面跟有数字的“⑪”
var textWithoutSpecialChar = processedText.replace(/⑪(?=\d)/g, '');

return textWithoutSpecialChar;

}

function toHalfWidth(str) { return str.split('').map(function(char) { var code = char.charCodeAt(0);

    // 全角空格的特殊处理
    if (code === 0x3000) {
        return String.fromCharCode(0x0020);
    }

    // 处理其他全角字符(除空格外)
    if (code >= 0xFF01 && code <= 0xFF5E) {
        return String.fromCharCode(code - 0xFEE0);
    }

    // 其他字符保持不变
    return char;
}).join('');

}

wakewon commented 7 months ago

先简单做了一稿处理了下空格,后面有空了再完善。

/**
 * Format Chinese annotations
 * @author wakewon
 * @usage in annotation menu
 * @link https://github.com/windingwind/zotero-actions-tags/issues/265
 * @see https://github.com/windingwind/zotero-actions-tags/issues/265
 */

if (!item) {
  return;
}

return await EditAnnotation(item);

async function EditAnnotation(annotationItem) {
  if (!annotationItem.isAnnotation()) {
    return "Not an annotation item";
  }
  annotationItem.annotationText = await FormatText(annotationItem.annotationText);
  return `Success`;
}

async function FormatText(text) {
  // Remove spaces between Chinese characters, but keep spaces between English letters and digits.
  return text.replace(/\s+(?=[^a-zA-Z0-9])/g, "");
}
yzy1228682367 commented 7 months ago

感谢大佬!太牛了!