pypublib.markdown module

class pypublib.markdown.Html[source]

Bases: object

Generator for common HTML elements.

Provides static methods for creating HTML elements from Python strings. Useful for building HTML content programmatically.

static blockquote(text)[source]

Generate an HTML blockquote element.

Parameters:

text (str) – The quoted text.

Returns:

HTML blockquote element string.

Return type:

str

static br()[source]

Generate a line break element.

Returns:

HTML line break string.

Return type:

str

static code(code, language='')[source]

Generate an HTML code block element.

Parameters:
  • code (str) – The code content.

  • language (str, optional) – Programming language for syntax highlighting class. Defaults to empty string.

Returns:

HTML pre/code element string.

Return type:

str

static em(text)[source]

Generate HTML emphasis (italic) element.

Parameters:

text (str) – The text to emphasize.

Returns:

HTML emphasis element string.

Return type:

str

static h1(text)[source]

Generate an H1 heading. See header().

static h2(text)[source]

Generate an H2 heading. See header().

static h3(text)[source]

Generate an H3 heading. See header().

static h4(text)[source]

Generate an H4 heading. See header().

static h5(text)[source]

Generate an H5 heading. See header().

static h6(text)[source]

Generate an H6 heading. See header().

static header(text, level=1)[source]

Generate an HTML header element.

Parameters:
  • text (str) – The header text content.

  • level (int, optional) – Header level (1-6). Defaults to 1.

Returns:

HTML header element string (e.g., ‘<h1>…</h1>’).

Return type:

str

Example

>>> Html.header("Title", level=1)
'<h1>Title</h1>'
static hr()[source]

Generate a horizontal rule (line break) element.

Returns:

HTML horizontal rule string.

Return type:

str

static img(src, alt_text='Image')[source]

Generate an HTML image element.

Parameters:
  • src (str) – The image source URL or path.

  • alt_text (str, optional) – Alternative text for the image. Defaults to ‘Image’.

Returns:

HTML image element string.

Return type:

str

Generate an HTML anchor (link) element.

Parameters:
  • href (str) – The link URL.

  • text (str) – The link text displayed to the user.

  • class_name (str, optional) – CSS class name to apply. Defaults to None.

Returns:

HTML anchor element string.

Return type:

str

static nbsp(count=1)[source]

Generate non-breaking spaces.

Parameters:

count (int, optional) – Number of non-breaking spaces to generate. Defaults to 1.

Returns:

HTML non-breaking space entities string.

Return type:

str

static ol(items)[source]

Generate an ordered (numbered) HTML list.

Parameters:

items (list[str]) – List of items to include as list items.

Returns:

HTML ordered list string with <li> elements.

Return type:

str

static p(text, class_name=None)[source]

Generate an HTML paragraph element.

Parameters:
  • text (str) – The paragraph text content.

  • class_name (str, optional) – CSS class name to apply. Defaults to None.

Returns:

HTML paragraph string.

Return type:

str

Example

>>> Html.p("Hello world")
'<p>Hello world</p>'
>>> Html.p("Styled", class_name="intro")
'<p class="intro">Styled</p>'
static pagebreak(id_=1)[source]

Generate a page break element.

Creates a CSS-based page break that works in EPUB readers.

Parameters:
  • id (int, optional) – Identifier for the page break (for reference). Defaults to 1.

  • id_ (int)

Returns:

HTML div element with page-break styling.

Return type:

str

static strong(text)[source]

Generate HTML strong (bold) element.

Parameters:

text (str) – The text to make bold.

Returns:

HTML strong element string.

Return type:

str

static ul(items)[source]

Generate an unordered (bulleted) HTML list.

Parameters:

items (list[str]) – List of items to include as list items.

Returns:

HTML unordered list string with <li> elements.

Return type:

str

class pypublib.markdown.MarkdownConverter[source]

Bases: object

Convert simple Markdown syntax to HTML.

Provides methods to convert basic Markdown markup to HTML elements. Supports headers, lists, code blocks, images, blockquotes, and text formatting.

static convert(text)[source]

Convert Markdown text to HTML.

Performs complete Markdown parsing and converts all supported Markdown syntax to corresponding HTML elements.

Parameters:

text (str) – Markdown-formatted text.

Returns:

HTML string with all Markdown elements converted.

Return type:

str

Example

>>> markdown = "# Title\nThis is **bold** text"
>>> MarkdownConverter.convert(markdown)
static paragraphs(*texts)[source]

Generate multiple HTML paragraphs from text strings.

Parameters:

*texts (str) – Variable length argument list of paragraph texts.

Returns:

Multiple HTML paragraph elements joined with newlines.

Return type:

str

Example

>>> MarkdownConverter.paragraphs("First", "Second", "Third")
'<p>First</p>\n<p>Second</p>\n<p>Third</p>'
static parse(text)[source]

Parse Markdown text and generate HTML structures.

Processes Markdown text line by line and yields HTML elements for:
  • Headers (# to ######)

  • Unordered lists (* items)

  • Ordered lists (. or 1. items)

  • Code blocks (` ... `)

  • Images (![alt](url))

  • Blockquotes (> text)

  • Horizontal rules (—)

  • Paragraphs (plain text)

Parameters:

text (str) – Markdown-formatted text.

Yields:

str – HTML elements as strings.

Note

This is a generator function that yields HTML elements for each parsed Markdown block.

static strong_or_em(text)[source]

Replace Markdown bold and emphasis syntax with HTML tags.

Converts:
  • text to <strong>text</strong>

  • _text_ to <em>text</em>

Parameters:

text (str) – Text containing Markdown formatting.

Returns:

Text with HTML formatting applied.

Return type:

str

Example

>>> MarkdownConverter.strong_or_em("This is *bold* and _italic_")
'This is <strong>bold</strong> and <em>italic</em>'