From 0580eee0aa61dfbf78c6bc5bf80da21f27d2389b Mon Sep 17 00:00:00 2001 From: Maboroshy Date: Sat, 14 Oct 2017 18:06:57 +0400 Subject: [PATCH] Added sub-lists for numbered list --- list-maker/info.json | 10 ++-- list-maker/list-maker.qml | 106 +++++++++++++++++++++++++------------- 2 files changed, 76 insertions(+), 40 deletions(-) diff --git a/list-maker/info.json b/list-maker/info.json index 8b0a181..fee6ff3 100644 --- a/list-maker/info.json +++ b/list-maker/info.json @@ -1,10 +1,10 @@ { - "name": "List maker", - "identifier": "list-maker", - "script": "list-maker.qml", + "name": "Make list", + "identifier": "make-list", + "script": "make-list.qml", "authors": ["@Maboroshy"], "platforms": ["linux", "macos", "windows"], - "version": "0.0.1", + "version": "0.0.2", "minAppVersion": "17.07.8", - "description" : "This script adds toolbar buttons and context menu items to change selected text formatting:\n- make it an ordered list with numbers or letters;\n- make it an unordered list with markers of your choice;\n- clear any list formatting.\n\nUsing buttons on a list of will override it's formatting with the one you choose. If the first line is part of the ordered list, other lines will continue the list:\n\n2. First selected line\nSome new line\n3. Ordered list line\n- marked list line\na. A first line of another ordered list\nb. A second line of another ordered list\n\nWill become:\n\n2. First selected line\n3. Some new line\n4. Ordered list line\n5. marked list line\n6. A first line of another ordered list\n7. A second line of another ordered list\n\nUnordered list marks and letters used for ordered list can be altered in the script settings.\n\nYou can also replace letters with Chinese/Japanese numbers or any other characters to use them for ordered lists.\n" + "description" : "This script adds toolbar buttons and context menu items to change selected text formatting:\n- make it an ordered list with numbers or letters;\n- make it an unordered list with markers of your choice;\n- clear any list formatting.\n\nIdented lines in numbered list will become a sub-list (1.1., 1.2...). Using buttons on a list will override it's formatting with the one you choose. If the first line is part of the ordered list, other lines will continue the list:\n\n2. First selected line\n Some new line\n3. Ordered list line\n- marked list line\na. A first line of another ordered list\nb. A second line of another ordered list\n\nWill become:\n\n2. First selected line\n 2.1. Some new line\n3. Ordered list line\n4. marked list line\n5. A first line of another ordered list\n6. A second line of another ordered list\n\nUnordered list marks and letters used for ordered list can be altered in the script settings.\n\nYou can also replace letters with Chinese/Japanese numbers or any other characters to use them for ordered lists.\n" } diff --git a/list-maker/list-maker.qml b/list-maker/list-maker.qml index 9fc2ad7..c6ea9a2 100644 --- a/list-maker/list-maker.qml +++ b/list-maker/list-maker.qml @@ -8,24 +8,32 @@ import QOwnNotesTypes 1.0 */ Script { - property string orderedLetters - property string unorderedMarkers + property string setLetters + property string setMarkers + property bool useNestingLetters property variant settingsVariables: [ { - "identifier": "orderedLetters", + "identifier": "setLetters", "name": "Letters to use for ordered lists", "description": "Letters/symbol and their order to use for ordered list", "type": "string", "default": "abcdefghijklmnopqrstuvwxyz", }, { - "identifier": "unorderedMarkers", + "identifier": "setMarkers", "name": "Unordered list item markers", - "description": "Put the symbols you want to use as marked list item markers. Spaces and commas are ignored.", + "description": "Put the symbols you want to use as marked list item markers. Spaces and commas are ignored", "type": "string", "default": "- • ‣", - } + }, + { + "identifier": "useNestingLetters", + "name": "Use letters instead of numbers for second level of nested numbered list instead", + "description": "Enable for letters, disable for numbers", + "type": "boolean", + "default": "false", + }, ] property string letters @@ -34,27 +42,27 @@ Script { function init() { script.registerCustomAction("list 123", "1. 2. 3. list", "1.", "", true) - if (orderedLetters) { - letters = orderedLetters.replace(/[\s,;]/g, "") + if (setLetters) { + letters = setLetters.replace(/[\s,;]/g, "") var orderedLettersListIcon = letters[0] + "." var orderedLettersListName = "%1. %2. %3. list".arg(letters[0]).arg(letters[1]).arg(letters[2]) script.registerCustomAction("list abc", orderedLettersListName, orderedLettersListIcon, "", true) } - if (unorderedMarkers) { - markers = unorderedMarkers.replace(/[\s,;]/g, "") - for (var n = 0; n < markers.length; n++) { - if (markers[n] != " " && markers[n] != ",") - script.registerCustomAction("list " + markers[n], markers[n] + " list", markers[n]) - } + if (setMarkers) { + markers = setMarkers.replace(/[\s,;]/g, "") + for (var n = 0; n < markers.length; n++) + script.registerCustomAction("list " + markers[n], markers[n] + " list", markers[n]) } script.registerCustomAction("list " + markers[0], "%1 list".arg(markers[0]), "", "", true, true) script.registerCustomAction("list clear", "Clear list formatting", "X", "", true) } + // This will return the type of a list of the first line of text function getListType(text) { - if (text.search(/^\d+\. /) == 0) + text = text.replace(/^ */, "") + if (text.search(/\d[\.\d]*\. /) != -1) var type = "number" else if (letters.indexOf(text.substring(0, 1)) != -1 && text.substring(1, 3) == ". ") var type = "letter" @@ -66,17 +74,18 @@ Script { return type } + // This will clear the text of all list formatting the script uses function clearLine(text) { var line = text var lineType = getListType(line) while (lineType != "none") { if (lineType == "number") - line = line.replace(/^\d+\. /, "") + line = line.replace(/\d[\.\d]*\. /, "") else if (lineType == "letter") - line = line.substring(3) + line = line.replace(/[^ ]\. /, "") else if (lineType == "mark") - line = line.substring(2) + line = line.replace(/[^ ] /, "") lineType = getListType(line) } @@ -89,17 +98,34 @@ Script { var type = getListType(script.noteTextEditSelectedText()) var lines = script.noteTextEditSelectedText().split("\n") var newText = [] + lines[0] = lines[0].replace(/^ */, "") - if (action == "list 123" && type == "number") - number = lines[0].match(/^\d+/) - 1 - else if (action == "list abc" && type == "letter") - number = letters.indexOf(lines[0].substring(0, 1)) - else + if (action == "list 123" && type == "number") { + + // Continue the list for nested and flat numbered list + if (lines[0].search(/\d+\.\d+\. /) != -1) { + var number = lines[0].match(/^\d*/) + lines[0] = lines[0].replace(number + ".", "") + var subnumber = lines[0].match(/^\d*/) - 1 + lines[0] = " " + lines[0] + } + else { + var number = lines[0].match(/\d*/) - 1 + var subnumber = 0 + } + } + else if (action == "list abc" && type == "letter") { + var number = letters.indexOf(lines[0].substring(0, 1)) + var subnumber = 0 + } + else { var number = 0 - + var subnumber = 0 + } + for (var n = 0; n < lines.length; n++) { - if (lines[n] == "" || lines[n].search(/^\s/) != -1) { + if (lines[n] == "" || lines[n].substring(0, 1) == "\t") { newText.push(lines[n]) continue } @@ -107,19 +133,29 @@ Script { var line = (clearLine(lines[n])) if (action == "list clear") { - newText.push(line) + newText.push(line.replace(/^ */, "")) continue } - else if (action == "list 123") { - number++ - var mark = number + ". " - } - else if (action == "list abc") - var mark = letters[number++] + ". " - else - var mark = action.substring(5, 6) + " " + else if (action == "list 123") { - newText.push(mark + line) + if (line.substring(0, 1) == " ") { + subnumber++ + var mark = " " + number + "." + subnumber + ". " + } + else { + subnumber = 0 + number++ + var mark = number + ". " + } + } + else if (action == "list abc") { + var mark = letters[number++] + ". " + } + else { + var mark = action.substring(5, 6) + " " + } + + newText.push(mark + line.replace(/^ */, "")) } script.noteTextEditWrite(newText.join("\n"))