|
21 | 21 | RegExNonCapturing = r"{\w:\d+:?.*?}" |
22 | 22 | # The basic format of the references |
23 | 23 | EmptyRef = "{{{}:{}:{}}}" |
| 24 | +EmptyRefSearchable = "{{{}:{}:" |
24 | 25 | PersoLetter = "C" |
25 | 26 | TextLetter = "T" |
26 | 27 | PlotLetter = "P" |
27 | 28 | WorldLetter = "W" |
28 | 29 |
|
29 | 30 |
|
30 | | -def plotReference(ID): |
31 | | - """Takes the ID of a plot and returns a reference for that plot.""" |
32 | | - return EmptyRef.format(PlotLetter, ID, "") |
| 31 | +def plotReference(ID, searchable=False): |
| 32 | + """Takes the ID of a plot and returns a reference for that plot. |
| 33 | + @searchable: returns a stripped version that allows simple text search.""" |
| 34 | + if not searchable: |
| 35 | + return EmptyRef.format(PlotLetter, ID, "") |
| 36 | + else: |
| 37 | + return EmptyRefSearchable.format(PlotLetter, ID, "") |
33 | 38 |
|
34 | 39 |
|
35 | | -def persoReference(ID): |
36 | | - """Takes the ID of a character and returns a reference for that character.""" |
37 | | - return EmptyRef.format(PersoLetter, ID, "") |
| 40 | +def persoReference(ID, searchable=False): |
| 41 | + """Takes the ID of a character and returns a reference for that character. |
| 42 | + @searchable: returns a stripped version that allows simple text search.""" |
| 43 | + if not searchable: |
| 44 | + return EmptyRef.format(PersoLetter, ID, "") |
| 45 | + else: |
| 46 | + return EmptyRefSearchable.format(PersoLetter, ID, "") |
38 | 47 |
|
39 | 48 |
|
40 | | -def textReference(ID): |
41 | | - """Takes the ID of an outline item and returns a reference for that item.""" |
42 | | - return EmptyRef.format(TextLetter, ID, "") |
| 49 | +def textReference(ID, searchable=False): |
| 50 | + """Takes the ID of an outline item and returns a reference for that item. |
| 51 | + @searchable: returns a stripped version that allows simple text search.""" |
| 52 | + if not searchable: |
| 53 | + return EmptyRef.format(TextLetter, ID, "") |
| 54 | + else: |
| 55 | + return EmptyRefSearchable.format(TextLetter, ID, "") |
43 | 56 |
|
44 | 57 |
|
45 | | -def worldReference(ID): |
46 | | - """Takes the ID of a world item and returns a reference for that item.""" |
47 | | - return EmptyRef.format(WorldLetter, ID, "") |
| 58 | +def worldReference(ID, searchable=False): |
| 59 | + """Takes the ID of a world item and returns a reference for that item. |
| 60 | + @searchable: returns a stripped version that allows simple text search.""" |
| 61 | + if not searchable: |
| 62 | + return EmptyRef.format(WorldLetter, ID, "") |
| 63 | + else: |
| 64 | + return EmptyRefSearchable.format(WorldLetter, ID, "") |
48 | 65 |
|
49 | 66 |
|
50 | 67 | ############################################################################### |
@@ -375,7 +392,10 @@ def tooltip(ref): |
375 | 392 |
|
376 | 393 | item = idx.internalPointer() |
377 | 394 |
|
378 | | - tt = qApp.translate("references", "Text: <b>{}</b>").format(item.title()) |
| 395 | + if item.isFolder(): |
| 396 | + tt = qApp.translate("references", "Folder: <b>{}</b>").format(item.title()) |
| 397 | + else: |
| 398 | + tt = qApp.translate("references", "Text: <b>{}</b>").format(item.title()) |
379 | 399 | tt += "<br><i>{}</i>".format(item.path()) |
380 | 400 |
|
381 | 401 | return tt |
@@ -451,11 +471,32 @@ def linkifyAllRefs(text): |
451 | 471 | return re.sub(RegEx, lambda m: refToLink(m.group(0)), text) |
452 | 472 |
|
453 | 473 |
|
| 474 | +def findReferencesTo(ref, parent=None, recursive=True): |
| 475 | + """List of text items containing references ref, and returns IDs. |
| 476 | + Starts from item parent. If None, starts from root.""" |
| 477 | + oM = mainWindow().mdlOutline |
| 478 | + |
| 479 | + if parent == None: |
| 480 | + parent = oM.rootItem |
| 481 | + |
| 482 | + # Removes everything after the second ':': '{L:ID:random text}' → '{L:ID:' |
| 483 | + ref = ref[:ref.index(":", ref.index(":") + 1)+1] |
| 484 | + |
| 485 | + # Bare form '{L:ID}' |
| 486 | + ref2 = ref[:-1] + "}" |
| 487 | + |
| 488 | + # Since it's a simple search (no regex), we search for both. |
| 489 | + lst = parent.findItemsContaining(ref, [Outline.notes.value], recursive=recursive) |
| 490 | + lst += parent.findItemsContaining(ref2, [Outline.notes.value], recursive=recursive) |
| 491 | + |
| 492 | + return lst |
| 493 | + |
454 | 494 | def listReferences(ref, title=qApp.translate("references", "Referenced in:")): |
455 | 495 | oM = mainWindow().mdlOutline |
456 | 496 | listRefs = "" |
457 | | - ref = ref[:ref.index(":", ref.index(":") + 1)] |
458 | | - lst = oM.findItemsContaining(ref, [Outline.notes.value]) |
| 497 | + |
| 498 | + lst = findReferencesTo(ref) |
| 499 | + |
459 | 500 | for t in lst: |
460 | 501 | idx = oM.getIndexByID(t) |
461 | 502 | listRefs += "<li><a href='{link}'>{text}</a></li>".format( |
|
0 commit comments