Skip to content

ngdb.parser

Norton Guide parser for the text inside a guide.

CTRL_CHAR module-attribute

CTRL_CHAR = '^'

The control character that marks an upcoming attribute.

BaseParser

BaseParser(line)

The base text parsing class.

Parameters:

Name Type Description Default

line

str

The raw string to parse.

required

bold

bold()

Handle being asked to go to bold mode.

char

char(char)

Handle an individual character value.

Parameters:

Name Type Description Default

char

int

The character value to handle.

required

colour

colour(colour)

Handle the given colour value.

Parameters:

Name Type Description Default

colour

int

The colour value to handle.

required

normal

normal()

Handle being asked to go to normal mode.

reverse

reverse()

Handle being asked to go to reverse mode.

text

text(text)

Handle the given text.

Parameters:

Name Type Description Default

text

str

The text to handle.

required

unbold

unbold()

Handle being asked to go out of bold mode.

underline

underline()

Handle being asked to go in underline mode.

unreverse

unreverse()

Handle being asked to go out of reverse mode.

ununderline

ununderline()

Handle being asked to go out of underline mode.

MarkupText

MarkupText(line)

Bases: PlainText, ABC

Read a line of Norton Guide text and mark up with start/end tags.

This is an abstract base class for other parser classes that will implement start and end tags where necessary.

begin_markup

begin_markup(cls)

Start a section of markup.

Parameters:

Name Type Description Default

cls

str

The class for the markup.

required
Note

As a side-effect of calling on this, the close_markup for the same class will be placed on an internal stack, for use when end_markup is called.

close_markup abstractmethod

close_markup(cls)

Close markup for the given class.

Parameters:

Name Type Description Default

cls

str

The class of thing to close the markup for.

required

Returns:

Type Description
str

The closing markup text.

end_markup

end_markup()

End a section of markup.

normal

normal()

Handle being asked to go to normal mode.

Note

Internally this also clears the whole stack of closing tags.

open_markup abstractmethod

open_markup(cls)

Open markup for the given class.

Parameters:

Name Type Description Default

cls

str

The class of thing to open the markup for.

required

Returns:

Type Description
str

The opening markup text.

ParseState

ParseState(line)

Raw text parsing state tracking class.

Parameters:

Name Type Description Default

line

str

The line to work on.

required

ctrl instance-attribute

ctrl = find(CTRL_CHAR)

The location of the next control marker.

ctrl_id property

ctrl_id

The current control ID.

last_attr instance-attribute

last_attr = -1

The last attribute encountered.

mode instance-attribute

mode = NORMAL

The current mode.

raw instance-attribute

raw = line

The current raw text that's left to handle.

work_left property

work_left

Is there any work left to do?

PlainText

PlainText(line)

Bases: BaseParser

Read a line of Norton Guide text as plain text.

__str__

__str__()

Return the plain text of the line.

Returns:

Type Description
str

The parsed line, as plan text.

RichText

RichText(line)

Bases: MarkupText

Read a line of Norton Guide text and mark up with Rich console markup.

Note

This is implemented in a way that doesn't require that Rich is a dependency of this library. This is provided here as a test and a handy example, and one that uses Rich's plain text BBCode-a-like markup.

COLOUR_MAP class-attribute instance-attribute

COLOUR_MAP = {
    1: 4,
    3: 6,
    4: 1,
    6: 3,
    9: 21,
    11: 14,
    12: 196,
    14: 11,
}

DOS to Rich colour mapping. This is just the exceptions.

map_colour classmethod

map_colour(colour)

Map a DOS colour into a similar colour from Rich.

Parameters:

Name Type Description Default

colour

int

The DOS colour to map from.

required

Returns:

Type Description
int

The mapped colour.

TextMode

Bases: Enum

Types of text mode used when parsing a Norton Guide line.

ATTR class-attribute instance-attribute

ATTR = auto()

Raw colour attribute.

BOLD class-attribute instance-attribute

BOLD = auto()

Bold text.

NORMAL class-attribute instance-attribute

NORMAL = auto()

Normal text.

REVERSE class-attribute instance-attribute

REVERSE = auto()

Reverse text.

UNDERLINE class-attribute instance-attribute

UNDERLINE = auto()

Underlined text.