Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for markdown output in help messages #29

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions example/src/clic_ex-commands-subsub.adb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ package body CLIC_Ex.Commands.Subsub is
Put_Line => Ada.Text_IO.Put_Line,
Put_Error => Ada.Text_IO.Put_Line,
Error_Exit => GNAT.OS_Lib.OS_Exit,
TTY_Chapter => CLIC.TTY.Info,
TTY_Description => CLIC.TTY.Description,
TTY_Version => CLIC.TTY.Version,
TTY_Underline => CLIC.TTY.Underline,
TTY_Emph => CLIC.TTY.Emph);
TTY_Chapter => CLIC.Formatter.Chapter,
TTY_Description => CLIC.Formatter.Description,
TTY_Version => CLIC.Formatter.Version,
TTY_Underline => CLIC.Formatter.Underline,
TTY_Emph => CLIC.Formatter.Emph);
begin
Sub.Register (new Sub.Builtin_Help);
Sub.Register (new CLIC_Ex.Commands.TTY.Instance);
Expand Down
20 changes: 17 additions & 3 deletions example/src/clic_ex-commands.adb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
with AAA.Strings;

with CLIC.TTY;
with CLIC.Formatter;
with CLIC.User_Input;
with CLIC.Config.Load;

Expand All @@ -22,6 +22,9 @@ package body CLIC_Ex.Commands is
No_TTY : aliased Boolean := False;
-- Used to disable control characters in output

Markdown_Help : aliased Boolean := False;
-- Used to enable help display in markdown format

-------------------------
-- Set_Global_Switches --
-------------------------
Expand Down Expand Up @@ -50,6 +53,13 @@ package body CLIC_Ex.Commands is
No_TTY'Access,
Long_Switch => "--no-tty",
Help => "Disables control characters in output");

Define_Switch (Config,
Markdown_Help'Access,
Long_Switch => "--markdown",
Help =>
"Enables output of markdown format for help");

end Set_Global_Switches;

-------------
Expand All @@ -61,11 +71,15 @@ package body CLIC_Ex.Commands is
Sub_Cmd.Parse_Global_Switches;

if No_TTY then
CLIC.TTY.Force_Disable_TTY;
CLIC.Formatter.Force_Disable_TTY;
end if;

if Markdown_Help then
CLIC.Formatter.Enable_Markdown;
end if;

if not No_Color and then not No_TTY then
CLIC.TTY.Enable_Color (Force => False);
CLIC.Formatter.Enable_Color (Force => False);
-- This may still not enable color if TTY is detected to be incapable
end if;

Expand Down
12 changes: 6 additions & 6 deletions example/src/clic_ex-commands.ads
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
private with Ada.Text_IO;
private with GNAT.OS_Lib;
private with CLIC.Subcommand.Instance;
private with CLIC.TTY;
private with CLIC.Formatter;
private with CLIC.Config;

package CLIC_Ex.Commands is
Expand All @@ -24,9 +24,9 @@ private
Put_Line => Ada.Text_IO.Put_Line,
Put_Error => Ada.Text_IO.Put_Line,
Error_Exit => GNAT.OS_Lib.OS_Exit,
TTY_Chapter => CLIC.TTY.Info,
TTY_Description => CLIC.TTY.Description,
TTY_Version => CLIC.TTY.Version,
TTY_Underline => CLIC.TTY.Underline,
TTY_Emph => CLIC.TTY.Emph);
TTY_Chapter => CLIC.Formatter.Chapter,
TTY_Description => CLIC.Formatter.Description,
TTY_Version => CLIC.Formatter.Version,
TTY_Underline => CLIC.Formatter.Underline,
TTY_Emph => CLIC.Formatter.Emph);
end CLIC_Ex.Commands;
2 changes: 1 addition & 1 deletion src/clic-config.ads
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ package CLIC.Config with Preelaborate is
access function (Key : Config_Key; Value : TOML.TOML_Value)
return Boolean;
-- Return False when a Key/Value combination is not valid. Can be used to
-- check formating of string value like email address for instance.
-- check formatting of string value like email address for instance.

procedure Import (This : in out Instance;
Table : TOML.TOML_Value;
Expand Down
44 changes: 44 additions & 0 deletions src/clic-formatter.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
with CLIC.TTY;
with CLIC.Markdown;

package body CLIC.Formatter is

Markdown_Enabled : Boolean := False;

procedure Enable_Markdown is
begin
Markdown_Enabled := True;
end Enable_Markdown;

procedure Force_Disable_TTY renames TTY.Force_Disable_TTY;
procedure Enable_Color (Force : Boolean := False)
renames TTY.Enable_Color;

function Chapter (Str : String) return String is
(if Markdown_Enabled
then Markdown.Chapter (Str)
else TTY.Bold (Str));


function Description (Str : String) return String is
(if Markdown_Enabled
then Markdown.Code (Str)
else TTY.Description (Str));

function Version (Str : String) return String is
(if Markdown_Enabled
then Markdown.Italic (Str)
else TTY.Version (Str));

function Underline (Str : String) return String is
(if Markdown_Enabled
then Markdown.Bold (Str)
else TTY.Underline (Str));

function Emph (Str : String) return String is
(if Markdown_Enabled
then Markdown.Code (Str)
else TTY.Emph (Str));


end CLIC.Formatter;
22 changes: 22 additions & 0 deletions src/clic-formatter.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package CLIC.Formatter
with Preelaborate
is

-- Provides markdown or TTY formatting for displaying help pages.

procedure Force_Disable_TTY;

procedure Enable_Markdown;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me this is not the right way to add a Markdown output for the help pages. You may want to print the help in markdown without changing the formatting of the other outputs of your program.

Also it doesn't make much sens to outputting markdown for other parts of a command line program.

What I did for my [Man page POChttps://github.com//pull/12/files)) is to add a special Print_Man_Page to the CLIC.Subcommand.Instance package.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I like the idea of a Markdown output for the help :)

Copy link
Author

@mgrojo mgrojo Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My rationale for doing it through the formatting parameters of CLIC.Subcommand.Instance is that all the logic already present in Display_Help can be reused. In fact, when looking at that generic parameters, I figured out that something like I'm doing was envisioned.

For example, the recent change about the global switches (a6bea52) would have to be applied in two places if there is a different help page generation for both formats like in #12. Isn't it better if there is a single place where the page is composed and the format can be parameterized?

You may want to print the help in markdown without changing the formatting of the other outputs of your program.

But the help printing is a single purpose command. When you print the help page, either for generating a markdown format or for directly reading, this will be the single action performed by the application, it doesn't make sense to generate any other output.

Where I agree undoubtedly is that, instead of a global switch (--markdown) to be combined with --help as I thought it, it should be a single switch for a specific subcommand. I was influence by how --no-tty and similar switches worked and implemented it similarly, but always thinking in this --markdown switch to be used along with --help or the help subcommand, not for any other program output.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it better if there is a single place where the page is composed and the format can be parameterized?

This solution does work with TTY and Markdown, but it's not going to work for Man or HTML because of more complex output structures (lists in HTML for instance). So we will need another ad-hoc way to have those formats.

@mosteo up to you.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Markdown works well, HTML is indeed the final output of the former.

But, in any case, if @mosteo thinks the same, I don't have any problem in changing it to the other approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should emit a single format that is suitable for conversion with e.g. pandoc rather than having multiple generators. If the logic can be reused for the single chosen format, I'd prefer that, but if as @Fabien-Chouteau says, there are structures that won't work, then that forces our hand to have a separate generator.

The way I'd go about it would be to use a sample complex page to check both issues: that it can be converted properly and whether the existent logic can be reused.

-- Enables markdown output of formatting functions.

procedure Enable_Color (Force : Boolean := False);
-- Prepares colors for the terminal output. Unless Force, will do nothing
-- if a console redirection is detected.

function Chapter (Str : String) return String;
function Description (Str : String) return String;
function Version (Str : String) return String;
function Underline (Str : String) return String;
function Emph (Str : String) return String;

end CLIC.Formatter;
19 changes: 19 additions & 0 deletions src/clic-markdown.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

package body CLIC.Markdown is

function Chapter (Str : String) return String is
("## " & Str);

function Plain_Text (Str : String) return String is
(Str);

function Code (Str : String) return String is
('`' & Str & '`');

function Bold (Str : String) return String is
('*' & Str & '*');

function Italic (Str : String) return String is
('_' & Str & '_');

end CLIC.Markdown;
12 changes: 12 additions & 0 deletions src/clic-markdown.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

package CLIC.Markdown
with Preelaborate
is

function Chapter (Str : String) return String;
function Plain_Text (Str : String) return String;
function Code (Str : String) return String;
function Bold (Str : String) return String;
function Italic (Str : String) return String;

end CLIC.Markdown;
2 changes: 1 addition & 1 deletion src/clic-subcommand-instance.adb
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ package body CLIC.Subcommand.Instance is
Table.Append (TTY_Description ("<arguments>"));
Table.Append ("List of arguments for the command");

Table.Print (Separator => " ",
Table.Print (Separator => " ",
Put_Line => Put_Line_For_Access'Access);
end;

Expand Down
6 changes: 4 additions & 2 deletions src/clic-subcommand-instance.ads
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ generic
-- Used to signal that the program should terminate with the give error
-- code. Typicaly use GNAT.OS_Lib.OS_Exit.

-- The procedures below are used to format the output such as usage and
-- help. Use CLIC.Subcommand.No_TTY if you don't want or need formating.
-- The functions below are used to format the output such as usage and
-- help. Use CLIC.Subcommand.No_TTY if you don't want or need formatting.
-- CLIC also provides ready implementations in CLIC.Formatter, CLIC.TTY
-- and CLIC.Markdown.
with function TTY_Chapter (Str : String) return String;
with function TTY_Description (Str : String) return String;
with function TTY_Version (Str : String) return String;
Expand Down
2 changes: 1 addition & 1 deletion src/clic-subcommand.ads
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ package CLIC.Subcommand is
function No_TTY (Str : String) return String
is (Str);
-- Use this function for the TTY_* generic parameters of
-- CLIC.Subcommand.Instance if you don't want or need TTY formating.
-- CLIC.Subcommand.Instance if you don't want or need TTY formatting.

private

Expand Down
2 changes: 1 addition & 1 deletion src/clic-tty.ads
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ is

procedure Force_Disable_TTY
with Post => not Is_TTY;
-- Disable TTY support even if availabe
-- Disable TTY support even if available

--------------------
-- Color enabling --
Expand Down