danoan.correct_markdown.core.markdown_view module

class danoan.correct_markdown.core.markdown_view.DiffOperation(value)[source]

Bases: Enum

An enumeration.

Delete = 'delete'
Equal = 'equal'
Insert = 'insert'
Replace = 'replace'
class danoan.correct_markdown.core.markdown_view.MarkdownView(original_markdown: TextIO, keep_markdown_tags: bool = False)[source]

Bases: object

Find and replace plain-text content in a markdown string without disrupting markdown and html markups.

This class is designed to do replace operations in the plain-text content of a markdown string. All markdown and html markups should remain the same and not be affected by a replace operation.

The only exception is when the plain-text string being replaced is spread over two or more non-contiguous text segments. In this case, the new value is put in the first segment.

Parameters:
  • original_markdown (TextIO)

  • keep_markdown_tags (bool)

find(search_value: str, start: int | None = None, end: int | None = None, ignore_trailing_spaces: bool = True) Tuple[int, int][source]

Find the search value in the non new line text view and return the start and end indexes.

The start and end parameters limit the search to a substring. If ignore_trailing_spaces then “searched value” will match “searched value”.

Parameters:
  • search_value (str)

  • start (int | None)

  • end (int | None)

  • ignore_trailing_spaces (bool)

Return type:

Tuple[int, int]

get_full_content() str[source]

Return a string joining all pure markdown, HTML tags and plain-text segments.

Return type:

str

get_html_view() str[source]

Return a string joining HTML tags segments.

Return type:

str

get_no_html_view() str[source]

Return a string joining all pure markdown and plain-text segments.

Return type:

str

replace(t_start: int, old_value: str, new_value: str)[source]

Replace old value by new value in text view and update segments.

  1. Find the indexes ts,te in the text view that corresponding to the old value.

  2. Find the segments ms, me in the index that correspond to ts,te.

  3. If ms==me then the old value is within the same segment and we can do a simple replacement.

  4. If ms!=me then the old value is spread over more than one segment. We put the text content within (ms,me) segments of type text in the segment ms (the first one) and do the replace operation there. Next we erase the segments (ms+1…ms_end); and merge segments of equal type that become contiguous after the delete operation.

  5. We need to update the index.

Parameters:
  • t_start (int)

  • old_value (str)

  • new_value (str)

class danoan.correct_markdown.core.markdown_view.SegmentType(value)[source]

Bases: Enum

An enumeration.

EditableContent = 'no_html'
NoEditableContent = 'html'
danoan.correct_markdown.core.markdown_view.build_plain_text_segments(original_markdown: TextIO)[source]

Create StringView segments for a markdown document.

The EditableContent will contain plain text and no markdown markups. That is.

Example

original_markdown: <span>Today is a wonderful day!</span>

EditableContent: Today is a wonderful day! NoEditableContent: <span>****</span>

Parameters:

original_markdown (TextIO)

danoan.correct_markdown.core.markdown_view.build_pure_markdown_segments(original_markdown: TextIO)[source]

Create StringView segments for a markdown document.

The EditableContent will contain plain text and also markdown markups. That is.

Example

original_markdown: <span>Today is a wonderful day!</span>

EditableContent: Today is a wonderful day! NoEditableContent: <span></span>

Parameters:

original_markdown (TextIO)

danoan.correct_markdown.core.markdown_view.build_segments(segments: Dict[Any, Any], base_string: TextIO, compare_string: TextIO, replace, insert, equal, delete)[source]

Create StringView segments from a list of diffs between a compare string and a base string.

Example

base_string: <span>Today is a wonderful day!</span> compare_string: Today is a wonderful day!

diffs:
  • Insert(<span>)

  • Equal(Today is a wonderful day!)

  • Insert(</span>

The diff events are used to arbitrate how the string segments are created. In the example above, one could separate html tags from text content.

Parameters:
  • segments (Dict[Any, Any])

  • base_string (TextIO)

  • compare_string (TextIO)