ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
formatter.h File Reference

Groff/troff formatting utilities for man page generation. More...

Go to the source code of this file.

Functions

const char * manpage_fmt_escape_groff (const char *str)
 Escape special characters for groff output.
 
void manpage_fmt_write_section (FILE *f, const char *section_name)
 Write a section header directive.
 
void manpage_fmt_write_blank_line (FILE *f)
 Write a blank line (for spacing between sections)
 
void manpage_fmt_write_bold (FILE *f, const char *text)
 Write text in bold format.
 
void manpage_fmt_write_italic (FILE *f, const char *text)
 Write text in italic format.
 
void manpage_fmt_write_tagged_paragraph (FILE *f)
 Write a tagged paragraph header.
 
void manpage_fmt_write_text (FILE *f, const char *text)
 Write plain text line (without directive)
 
void manpage_fmt_write_title (FILE *f, const char *program_name, const char *mode_name, const char *brief_description)
 Write groff title/header (.TH directive)
 

Detailed Description

Groff/troff formatting utilities for man page generation.

This module provides utilities for generating properly formatted groff/troff output for man pages. Handles:

  • Section headers (.SH directive)
  • Text formatting (bold .B, italic .I, constant .C)
  • Paragraph and item formatting (.TP for tagged paragraphs)
  • Special character escaping
  • Section markers (AUTO/MANUAL/MERGE)

All functions write directly to FILE* for efficient streaming output.

Definition in file formatter.h.

Function Documentation

◆ manpage_fmt_escape_groff()

const char * manpage_fmt_escape_groff ( const char *  str)

Escape special characters for groff output.

Escapes characters that have special meaning in groff format. Currently returns the string as-is since most man page content doesn't contain problematic characters. Can be extended for more robust escaping if needed.

Parameters
[in]strString to escape (can be NULL)
Returns
Escaped string (never NULL, returns "" for NULL input)
Note
This is a simple implementation. For production use with arbitrary content, consider a more robust escaping strategy.

Definition at line 21 of file formatter.c.

21 {
22 // For simplicity, we'll just return the string as-is
23 // In a more robust implementation, we'd escape special characters
24 // But for man page content, most strings don't have problematic characters
25 return str ? str : "";
26}

Referenced by manpage_fmt_write_title().

◆ manpage_fmt_write_blank_line()

void manpage_fmt_write_blank_line ( FILE *  f)

Write a blank line (for spacing between sections)

Parameters
[in]fOutput file handle (cannot be NULL)

Definition at line 35 of file formatter.c.

35 {
36 if (!f) {
37 return;
38 }
39 fprintf(f, "\n");
40}

Referenced by options_config_generate_manpage_template().

◆ manpage_fmt_write_bold()

void manpage_fmt_write_bold ( FILE *  f,
const char *  text 
)

Write text in bold format.

Writes text with bold formatting directive. Example: manpage_fmt_write_bold(f, "ascii-chat") writes ".B ascii-chat\n"

Parameters
[in]fOutput file handle (cannot be NULL)
[in]textText to write in bold (cannot be NULL)

Definition at line 42 of file formatter.c.

42 {
43 if (!f || !text) {
44 return;
45 }
46 fprintf(f, ".B %s\n", text);
47}

◆ manpage_fmt_write_italic()

void manpage_fmt_write_italic ( FILE *  f,
const char *  text 
)

Write text in italic format.

Writes text with italic formatting directive. Example: manpage_fmt_write_italic(f, "options") writes ".I options\n"

Parameters
[in]fOutput file handle (cannot be NULL)
[in]textText to write in italic (cannot be NULL)

Definition at line 49 of file formatter.c.

49 {
50 if (!f || !text) {
51 return;
52 }
53 fprintf(f, ".I %s\n", text);
54}

◆ manpage_fmt_write_section()

void manpage_fmt_write_section ( FILE *  f,
const char *  section_name 
)

Write a section header directive.

Writes ".SH SECTION_NAME" directive to output. Example: manpage_fmt_write_section(f, "OPTIONS") writes ".SH OPTIONS\n"

Parameters
[in]fOutput file handle (cannot be NULL)
[in]section_nameSection name to write (cannot be NULL)

Definition at line 28 of file formatter.c.

28 {
29 if (!f || !section_name) {
30 return;
31 }
32 fprintf(f, ".SH %s\n", section_name);
33}

Referenced by options_config_generate_manpage_template().

◆ manpage_fmt_write_tagged_paragraph()

void manpage_fmt_write_tagged_paragraph ( FILE *  f)

Write a tagged paragraph header.

Writes ".TP" directive to start a tagged paragraph (for option descriptions). Should be followed by manpage_fmt_write_bold() for the tag and then regular text for the description.

Parameters
[in]fOutput file handle (cannot be NULL)

Definition at line 56 of file formatter.c.

56 {
57 if (!f) {
58 return;
59 }
60 fprintf(f, ".TP\n");
61}

◆ manpage_fmt_write_text()

void manpage_fmt_write_text ( FILE *  f,
const char *  text 
)

Write plain text line (without directive)

Writes text directly without any formatting directive. Useful for description text and content lines.

Parameters
[in]fOutput file handle (cannot be NULL)
[in]textText to write (can be NULL, in which case only newline written)

Definition at line 63 of file formatter.c.

63 {
64 if (!f) {
65 return;
66 }
67
68 if (text) {
69 fprintf(f, "%s\n", text);
70 } else {
71 fprintf(f, "\n");
72 }
73}

◆ manpage_fmt_write_title()

void manpage_fmt_write_title ( FILE *  f,
const char *  program_name,
const char *  mode_name,
const char *  brief_description 
)

Write groff title/header (.TH directive)

Writes the full title header for a man page with current date. Format: .TH NAME SECTION DATE SOURCE MANUAL

Parameters
[in]fOutput file handle (cannot be NULL)
[in]program_nameProgram name (e.g., "ascii-chat") (cannot be NULL)
[in]mode_nameMode name or NULL (e.g., "server", "client", or NULL for binary-level)
[in]brief_descriptionOne-line description (cannot be NULL)

Definition at line 75 of file formatter.c.

75 {
76 if (!f || !program_name || !brief_description) {
77 return;
78 }
79
80 time_t now = time(NULL);
81 struct tm tm_buf;
82 platform_localtime(&now, &tm_buf);
83 char date_str[32];
84 strftime(date_str, sizeof(date_str), "%B %Y", &tm_buf);
85
86 // Build full program name (e.g., "ascii-chat-server" or just "ascii-chat")
87 char full_name[256];
88 if (mode_name) {
89 safe_snprintf(full_name, sizeof(full_name), "%s-%s", program_name, mode_name);
90 } else {
91 safe_snprintf(full_name, sizeof(full_name), "%s", program_name);
92 }
93
94 // .TH NAME SECTION DATE SOURCE MANUAL
95 // Section 1 = user commands, 5 = file formats
96 fprintf(f, ".TH %s 1 \"%s\" \"%s\" \"User Commands\"\n", full_name, date_str, program_name);
97 fprintf(f, ".SH NAME\n");
98 fprintf(f, ".B %s\n", full_name);
99 fprintf(f, "\\- %s\n", manpage_fmt_escape_groff(brief_description));
100 fprintf(f, "\n");
101}
const char * manpage_fmt_escape_groff(const char *str)
Escape special characters for groff output.
Definition formatter.c:21
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe formatted string printing to buffer.
Definition system.c:148
asciichat_error_t platform_localtime(const time_t *timer, struct tm *result)
Platform-safe localtime wrapper.
Definition util.c:50

References manpage_fmt_escape_groff(), platform_localtime(), and safe_snprintf().

Referenced by options_config_generate_manpage_template().