Formatting usage messages

前端 未结 4 1762
离开以前
离开以前 2021-02-03 14:04

If you take a look at the Combinatorica package in Mathematica8 in (mathematicapath)/AddOns/LegacyPackages/DiscreteMath/Combinatorica.m you wi

4条回答
  •  攒了一身酷
    2021-02-03 14:58

    I am currently working on rewriting your ApplicationMaker for newer Mathematica-Versions with added functionalities and came to the exact same question here.

    My answer is simple: Mathematica dont allowes you to use formated summaries for your symbols (or even build in symbols), so we have to unformate the usage-strings for the summaries. The usagestring itself can still have formatting, but one needs to have a function that removes all the formatingboxes from a string.

    i have a solution that uses the UndocumentedTestFEParserPacket as described by John Fultz! in this question.

    This funny named Tool parses a String Input into the real unchanged Mathematica BoxForm.

    This is my example code:

    str0 = Sum::usage
    
    str1=StringJoin[ToString[StringReplace[#, "\\\"" -> "\""]]& /@
    (Riffle[MathLink`CallFrontEnd[
    FrontEnd`UndocumentedTestFEParserPacket[str0, True]]〚1〛
    //. RowBox[{seq___}] :> seq /. BoxData -> List, " "]
    /. SubscriptBox[a_, b_] :> a<>"_"<>b
    /. Except[List, _Symbol][args__] :> Sequence@@Riffle[{args}, " "])];
    
    str2 = Fold[StringReplace, str1, 
    {((WhitespaceCharacter...)~~br:("["|"("|"=") ~~ (WhitespaceCharacter ...)) :> br,
    ((WhitespaceCharacter ...) ~~ br:("]"|"}"|","|".")) :> br, 
    (br:("{") ~~ (WhitespaceCharacter ...)) :> br, 
    ". " ~~ Except[EndOfString] -> ". \n"}]
    

    and this is how the Output looks like (first Output formatted fancy str0, second simple flat str2)

    first Output formatted fancy str0, second simple flat str2

    Code Explanation:

    str0 is the formatted usagestring with all the StyleBoxes and other formatting boxes.

    str1:

    UndocumentedTestFEParserPacket[str0, True] gives Boxes and strips off all StyleBoxes, thats because the second argument is True. First Replacement removes all RowBoxes. The outer BoxForm changed to a List of strings. Whitespaces are inserted between these strings the by Riffle. SubscriptBox gets a special treatment. The last line replaces every remaining FormatBox such as UnderoverscriptBox and it does that by adding Whitespaces between the arguments, and returning the arguments as a flat Sequence.

    ToString[StringReplace[#, "\\\"" -> "\""]]& /@
    

    was added to include more cases such as StringReplace::usage. This cases include string representations "" with Styles inside of a the usage-string, when "args" has to be given as strings.

    str2:

    In this block of code i only remove unwanted WhitespaceCharacter from the string str1 and i add linebreaks "/n" after the ".", because they got lost during the Parsing. There are 3 different cases where WhitespaceCharacter can be removed. 1 removing left-and right sided WithespaceCharacter from a character like "[". 2. and 3. removing WithespaceCharacter from left(2) or right(3) side.

    Summary

    Istead of summary-> mySymbol::usage, use summary -> unformatString[mySymbol::usage] with unformatString being an appropriate function that performes the unformating like descriped above.

    Alternatively you can define another usage message manually like

    f::usage = "fancy string with formating";
    f::usage2 = "flat string without formating";
    

    than use summary -> mySymbol::usage2

提交回复
热议问题