Last active
November 8, 2021 22:39
-
-
Save jbasko/d28db0f758b32ce385a20463a86090b2 to your computer and use it in GitHub Desktop.
Ģenerējam primitīvu jēdzienu rādītāju InDesign
This file contains hidden or 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
/** | |
* Generate index based on multiple inflections of the same term. | |
* Requires http://underscorejs.org/ (with export statement removed) to be placed in the specified path. | |
* | |
*/ | |
#target indesign; | |
#include "/path/to/underscore.js" | |
var _ = this._; | |
function main () { | |
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll; | |
if (app.documents.length !== 0 && app.activeDocument.stories.length !== 0) { | |
run(app.activeDocument) | |
} else { | |
alert("No documents are open or the document does not contain any text") | |
} | |
} | |
function capitalise(s) { | |
return s.charAt(0).toUpperCase() + s.substring(1).toLowerCase(); | |
} | |
function run (doc) { | |
var kodokaGroup = doc.paragraphStyleGroups.itemByName("Kodoka") | |
var paragraphStyle = kodokaGroup.paragraphStyles.itemByName("Index of Terms Paragraph") | |
var TERM_PAGES_DELIMITER = " "; // Special characters don't work, probably need to grep replace to use them. | |
var textFrameStyle = doc.objectStyleGroups.itemByName("Kodoka").objectStyles.itemByName("Index of Terms Box") | |
var TERMS = [ | |
{ | |
term: ["demokrātija", "Rietumu"], | |
ngram: ["rietum", "demokrāt"], | |
}, | |
// { | |
// term: "veselības aprūpe", | |
// sort: "veselibas aprupe", | |
// ngram: ["veselīb", "aprūp"], | |
// }, | |
{ | |
term: ["veselības aprūpe", "un labdarība"], | |
sort: "veselibas aprupe un labdariba", | |
dgrams: [["veselīb", "aprūp", "labdarīb"], ["labdarīb", "veselīb", "aprūp"]], | |
}, | |
{ | |
term: "sociālisms", | |
root: "sociālism", | |
}, | |
{ | |
term: "sociālais taisnīgums", | |
ngram: ["sociāl", "taisnīgum"], | |
}, | |
{ | |
term: "Raskins, Džons", | |
root: "raskin", | |
}, | |
{ | |
term: "Ļeņins", | |
sort: "lenins", | |
root: "ļeņin", | |
}, | |
{ | |
term: "Mao", | |
}, | |
{ | |
term: "Bērks, Edmunds", | |
root: "bērk", | |
}, | |
{ | |
term: "Taunzends, Pīters", | |
root: "taunzend", | |
}, | |
{ | |
term: "Aristotels", | |
root: "aristote", | |
}, | |
{ | |
term: "veci cilvēki", | |
dgram: ["vec", "cilvēk"], | |
}, | |
{ | |
term: "noziedzība", | |
root: "noziedz", | |
}, | |
] | |
_.forEach(TERMS, function(term) { | |
var originalTerm = term.term; | |
if (!_.isArray(originalTerm)) { | |
originalTerm = [originalTerm] | |
} | |
if (originalTerm.length === 1) { | |
term.cat = originalTerm[0]; | |
term.term = originalTerm[0]; | |
} else { | |
term.cat = originalTerm[0]; | |
term.term = originalTerm[1]; | |
} | |
if (!term.infl) { | |
term.infl = [] | |
} | |
if (term.root) { | |
term.infl.push("\\W" + term.root + "\\w+"); | |
} | |
if (!term.cat) { | |
term.cat = term.term; | |
} | |
if (!term.sort) { | |
if (term.cat === term.term) { | |
term.sort = term.term + " 000 " + term.term; | |
} else { | |
term.sort = term.cat + " " + term.term; | |
} | |
} | |
term.sort = term.sort.toLowerCase() | |
// N-grams | |
if (term.ngram) { | |
term.ngrams = [term.ngram]; | |
} | |
if (term.ngrams) { | |
_.forEach(term.ngrams, function (ng) { | |
term.infl.push("\\W+" + ng.join("\\w+ ") + "\\w+"); | |
}) | |
} | |
// Distant ngram | |
if (term.dgram) { | |
term.dgrams = [term.dgram] | |
} | |
if (term.dgrams) { | |
_.forEach(term.dgrams, function (dg) { | |
term.infl.push(dg.join("\\w+.+") + "\\w+"); | |
}) | |
} | |
// Default to term itself | |
if (term.infl.length === 0) { | |
term.infl.push(term.term) | |
} | |
}) | |
var results = {} | |
_.forEach(TERMS, function (term) { | |
// Initialise results | |
results[term.term] = {term: term.term, pages: []} | |
_.forEach(term.infl, function (infl) { | |
// Clear find/change preferences | |
app.findGrepPreferences = NothingEnum.nothing; | |
app.findGrepPreferences.findWhat = "(?i)" + infl; // Case insensitive! | |
var result = doc.findGrep(); | |
_.forEach(result, function (res) { | |
var pageNumber = res.parentTextFrames[0].parentPage.name; | |
results[term.term].pages.push(parseInt(pageNumber)) | |
}) | |
}) | |
}) | |
var resultsFrame = doc.textFrames.add({ | |
geometricBounds: [0, 0, 100, 100], | |
}); | |
function pushMergedPage(start, end, mergedPages) { | |
if (start === undefined) { | |
return; | |
} | |
if (start === end) { | |
mergedPages.push(start); | |
} else { | |
mergedPages.push(start + "-" + end); | |
} | |
} | |
var lastCat = undefined; | |
var contentsList = ""; | |
var paragraphIndices = [] | |
_.forEach(_.sortBy(TERMS, function (t) {return t.sort}), function (t) { | |
var uniquePages = _.unique(results[t.term].pages); | |
if ((uniquePages).length === 0) { | |
return | |
} | |
var sortedPages = _.sortBy(uniquePages); | |
var mergedPages = []; | |
var lastStart = undefined; | |
var lastEnd = undefined; | |
_.forEach(sortedPages, function (p) { | |
if (lastStart === undefined) { | |
lastStart = p; | |
lastEnd = p; | |
} else if (lastEnd + 1 === p) { | |
lastEnd = p; | |
} else { | |
pushMergedPage(lastStart, lastEnd, mergedPages); | |
lastStart = p; | |
lastEnd = p; | |
} | |
}) | |
pushMergedPage(lastStart, lastEnd, mergedPages); | |
if (t.term === t.cat) { | |
contentsList += "\r"; | |
paragraphIndices.push(contentsList.length); | |
contentsList += t.term + TERM_PAGES_DELIMITER + mergedPages.join(", "); | |
} else { | |
// The offset is achieved with indentation in the paragraph style, not with special characters. | |
if (lastCat !== t.cat) { | |
contentsList += "\r"; | |
paragraphIndices.push(contentsList.length); | |
contentsList += t.cat + "\n" | |
} else { | |
contentsList += "\n" | |
} | |
contentsList += t.term + TERM_PAGES_DELIMITER + mergedPages.join(", "); | |
} | |
lastCat = t.cat; | |
}); | |
resultsFrame.contents = contentsList.substring(1); // Strip the first newline | |
_.forEach(paragraphIndices, function (pi) { | |
resultsFrame.characters.itemByRange(pi, pi + 1).appliedParagraphStyle = paragraphStyle; | |
}) | |
resultsFrame.applyObjectStyle(textFrameStyle); | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment