Tinderbox Forum

Extract Part of Text, show this text in another note text

Hello,
how can I extract part of a text and show the ^value^ in another note’s text please?
Example attached. I’d like to extract the section in red in the volume note into the other note.
Thank you.
Extract Part of Text.tbx (106.8 KB)

Thanks for the test file - having a reference sample is a big help to helping you.

You ask for the section in red text. However, Tinderbox’s text search tools work on plain text so are blind to visual styling like coloured text, bolding, etc., that can’t be searched for. But…

This begs the question as to how the text got to be red. If you are marking it, then you are equally able to insert marker srting you can search for. Which leads to the next question…

Is the text of interest (i.e. the red text) only ever one paragraph? If it is we might insert a string like ‘#@#’ at the beginning of the paragraph. If the text of interest is one or more contiguous paragraphs, we would but the marker at beginning end of the text. If it is one on more non-contiguous pragraphs, then it’s likely possible but more work.

Of, if the text of interest is the paragraphs after the paragraph “Answer” and before the paragraph starting “Ref:” you could use those markers to find the text.

So, though you can’t search for red text, there are other markers you might search for or which you can add to enable you to use search.

Thank you Mark. Sorry, I didn’t explain properly. The colour is not relevant. It was an attempt to highlight that the sentence I wanted to extract to show in another note.
So, If I understood correctly, I could have the sentence starting with, let’s say #@#, them I could have an expression in another note with ^value^ of the text starting with #@#

Yes, in this file Extract Part of Text-ed.tbx (95.0 KB), view the preview and export panes of note Export. The two user attributes in that note tell the export template which note to look at (assumes that $Name is unique in the doc - if not use $Path instead). This data enables action code in the export code to do the heavy lift:

^action(
var:string vSource = $Text($TargetNote);
var:string vExtract;
if(vSource.contains("#@#([^\n]+)\n")){
   vExtract = $1;
};
)^

Basically the target note’s $Text is tested for an instance of “#@#” and if found everything up to the first line break is matched and held as a back-reference. This back reference $1 is then passed into the ex[ported content using a ^value()^ call and the new v9.3.0+ ability for ^value()^ to inset an action code variable’s value.

Thank you Mark. That’s great. I have the latest version of Tinderbox 9. I am struggling to put in an action prototype. Could please help me?
Thank you.
Much appreciated.

I’m note sure what you mean by this. Can you explain?

on an unrelated note, what are the chances… “#@#” is exactly the delimiter i use to mark text blocks in Tbx :smiley:

1 Like

FWIW, I used #@# as we need to stay clear of symbols that might have a regex significance and the popularity of Markdown inline mark-up means my old default of #### seems less same for general examples.

In truth, one might use a stting of letters—long enough to not have a normal use, say six Ns, NNNNNN. But many letters have a second symbolic life: in maths/science equations, general thought placeholders (X/Y/Z), N might imply a number.

Other keyboard options options, as little used (by most, not all): § ¶ † ‡ . So ‡‡‡‡ might be good as a split marker without being too visually intrusive. The latter is pertinent is I’ve often had content where I needed split/explode markers but still needed the source onte, and for it to be readable.

1 Like

Newspaper people used to confront a very similar problem, evolving some related abbreviations.

-30- : end of article
TK : placeholder, to be replaced with an actual figure or quote when reported

2 Likes

Good Mark. Could I put this code as action code in a note instead of html? If i had this action code in a prototype, i could have the extracted text that starts with #@# for some notes. Thank you.

Right, see this file Extract Part of Text-ed 2.tbx (98.6 KB)

This puts the text in red (marked with a #@# prefix) into $Text of notes #2 in the outline. But, that’s clunky, let’s make a prototype for the ‘answer’ note. same outcome but now a root-level configuration note /TBXConfig holds the regex marker string for us so all notes using the ‘pAnswerNote’ prototype will look for the same marker in the note stored in their own $TargetNote (which is made a Displayed Attribute via the prototype). As requested the action is now in the note as an edict (again, inherited from the prototype).

OK, but what if we’d like to take a different tack and do the work form the source note but this time shower the answer in an ‘Answers’ container. Again we use a prototype and the config note and we have this Extract Part of Text-ed 4.tbx (103.4 KB)

Summary:

  • first example above (in _this _ post) moves the answer extraction code from an export template into the $Edict of the note storing the answer text.
  • the second example does the same but parameterising things via use of a prototype so many notes can use the same code.
  • the third example uses the prototype approach of example 2 but implements the task a different way, running (inherited) edict code on the original note and storing the answer in a new note.

Observations:

  • I use edicts as these run often enough to capture can and can be run on demand of not, but running a a rule would be over-doing the task.
  • The regex only matches a single paragraph. Capturing multiple paragraphs will require a change to the regex.

One suggestion is to put the same #@# marker at the beginning and end of the ‘answer’ text and use this regex #@#([^#@#]+). Can this be done, of course, here it is: Extract Part of Text-ed 5.tbx (105.5 KB)

This is incredible. I just had a quick glance at the tbx files and they are exactly what I needed. I can’t wait to get home tonight to play with these.
Thank you very much. Much appreciated. Pascal.

1 Like