5
$\begingroup$

I have a template notebook that I'd like to substitute a string, but the string could possibly contain a Hyperlink.

Here is an example template notebook:

template notebook


I create a hyperlink to a help page with:

Hyperlink["Association", "ref/Association"]

That creates the hyperlinked text which I then paste into the GenerateDocument command.

GenerateDocument["testingTemplateNB.nb",
  <|
    "aString" -> "This is a string with a link to an Association"
  |>
 ]

The word "Association" is the hyperlink.

But when I generate the notebook from the template, it doesn't make the hyperlink. This is the text that is generated:

This is a string with a link to an \!\(\*TemplateBox[{"\"Association\"",{"\!\(\*Cell[TextData[ButtonBox[\"ref/Association\",BaseStyle->\"ExampleLink\",ButtonData->\"CellID\"]]]\)", None},"\!\(\*Cell[TextData[ButtonBox[\"ref/Association\",BaseStyle->\"ExampleLink\",ButtonData->\"CellID\"]]]\)"},"HyperlinkDefault"]\)

How do I put a link into a notebook generated from a template?

$\endgroup$
2
  • $\begingroup$ The least cumbersome way I found was to add a second slot and run GenerateDocument[nb, <|"aString" -> "text ", "link" -> Hyperlink["Associations", "ref/Associations"]|>]. $\endgroup$
    – Kuba
    Feb 21, 2020 at 6:46
  • $\begingroup$ @Kuba That's a good idea but in my case the text can be arbitrary and may or may not contain a link anywhere in the string. So I'm looking for the more general solution. $\endgroup$
    – Sean Lynch
    Feb 22, 2020 at 1:59

1 Answer 1

2
+50
$\begingroup$

Hyperlinks in text cells are an inline cells with standard form. I don't see how to pass this via notebook template slot (together with regular text for that cell). TextData can be a list but as soon as you put something else than _String to the template slot, it will end up a standard form as a whole.

So here is another alternative:

Create new Input cell with:

enter image description here

Then set the behavior to "Evaluate and delete".

Now you can run:

printTextCell[textData : _List | _String, style_: "Text"] := NotebookWrite[
   EvaluationNotebook[], 
   Cell[
     TextData[ 
       textData // Replace[#, std : Except[_String] :> Cell@BoxData@ToBoxes@std, {1}] & 
     ],
     style
   ]
];

GenerateDocument[nb,
  <| 
     "aString" -> {
       "This is a string with a link to an ",  Hyperlink["Association", "ref/Association"]
     }
  |>
]

enter image description here

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.