{"id":1234,"date":"2023-09-22T08:48:38","date_gmt":"2023-09-22T06:48:38","guid":{"rendered":"http:\/\/oopm.org\/?page_id=1234"},"modified":"2024-06-20T21:47:18","modified_gmt":"2024-06-20T19:47:18","slug":"6-x-a-simple-text-formatter-system","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=1234","title":{"rendered":"10.3-old A simple text formatter system"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages1234&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages1234&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">Here we present another example of using nested classes. The domain is text formatting as known from e.g. Microsoft Word, and LaTex. The complexity of most of these systems is huge. Here we will thus stick to a very simple text formatting system. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The phenomena of our domain are documents that consists of a number of paragraphs. The paragraphs of a document are formatted according to a given template, which defines a number of styles for formating the paragraphs in the document. A style may e.g. define the font being used, the size of the font, the form of the font, etc. A paragraph may be left-aligned, centred, right-aligned, og with flushed margins. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In addition to styles, there are also properties associated with a document like page orientation (portrait or landscape), paper size (like A4, A3, and US letter), and number of columns.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To avoid ending up with a large example, we will keep the possible template and style properties to a minimum. The text formatter we define, can handle documents with two types of paragraphs: headings and body text. Headings are automatically numbered sequentially and body texts are indented by three spaces. A document has only one property which is the maximum length of a line in a paragraph.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We start by defining class <code>Document<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code><strong>class<\/strong> Document(temp: <strong>ref<\/strong> Template, lineMax: <strong>var<\/strong> integer):\n    <strong>class<\/strong> Paragraph(theContent: <strong>var<\/strong> String, theStyle: <strong>ref<\/strong> Style):\n        format -> theStyledPar: <strong>var<\/strong> String:\n            theStyledPar :=\n                   theStyle.format(theContent.asString,lineMax)\n    content: <strong>obj<\/strong> OrderedList(Paragraph)    \n    addPar(P: <strong>ref<\/strong> Paragraph):\n       content.insert(P)\n    format -> formattedContent: <strong>var<\/strong> String:\n       theStyledPar := \"\"\n       content.scan \n          formattedContent := formattedContent + current.format\n    print:\n       print(format)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Class <code>Document<\/code> has two parameters <code>temp<\/code>, and <code>lineMax<\/code> where temp is the <code>Template<\/code> defining the format of the <code>Document<\/code>, and <code>lineMax<\/code> which defines the maximum length of a line in a paragraph. <code>Temp<\/code> and <code>lineMax<\/code> are used when adding paragraphs to a given document.<\/li>\n\n\n\n<li>It has a local class <code>Paragraph<\/code>, which has two parameters: <code>theContent<\/code> which is a  <code>String<\/code>, and <code>theStyle<\/code> which is a reference to a <code>Style<\/code> object defining the style of the paragraph.<\/li>\n\n\n\n<li>A <code>Paragraph<\/code> has a <code>format<\/code> method that invokes the <code>format<\/code> method of the <code>Style<\/code> with the <code>String<\/code> of the <code>Paragraph<\/code> and the <code>lineMax<\/code> as an argument. Class <code>Paragraph<\/code> is an example of a nested class.<\/li>\n\n\n\n<li>The data-item <code>content<\/code> holds the paragraphs of the <code>Document<\/code>.  The object <code>content<\/code> is of type <code>OrderedList(Paragraph)<\/code>. <code>OrderedList<\/code> is similar to class <code>Set<\/code> as described in Section 4. The difference is that there is no ordering of the elements in a <code>Set<\/code> object whereas the elements of an <code>OrderedList<\/code> are ordered in the order they are inserted into the list. For details about <code>OrderedList<\/code>,<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\"> see Section xxx<\/mark>.<\/li>\n\n\n\n<li>The method <code>addPar<\/code> adds a <code>Paragraph<\/code> to the <code>Document<\/code> by inserting it into <code>OrderedList<\/code>.<\/li>\n\n\n\n<li>The <code>print<\/code> method of <code>Document<\/code> invokes the <code>format<\/code> method and prints the <code>String<\/code> returned by <code>format<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we define class <code>Template<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code><strong>class<\/strong> Template:\n   styles:  <strong>obj<\/strong> OrderedList(Style) <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-secondary-color\">-- not used<\/mark>\n   heading: <strong>ref<\/strong> Style\n   bodyText: <strong>ref<\/strong> Style \n   inner(Template)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>Styles<\/code> defined for a given <code>Template<\/code> are held in a <code>styles<\/code> object of type <code>OrderedList<\/code>.<\/li>\n\n\n\n<li>A <code>Template<\/code> object has two styles <code>heading<\/code> and <code>bodyText<\/code> which must be defined in subclasses of <code>Template<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Next we define class <code>Style<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-tiny-font-size\"><code><strong>class<\/strong> Style -&gt; S: <strong>ref<\/strong> Style:\n   format(content: <strong>var<\/strong> String, lineMax: <strong>var<\/strong> integer) \n                                           -&gt; formattedContent: <strong>var<\/strong> String:&lt;\n      inner(format)\n   inner(Style)\n   S := this(Style)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this simple example, a <code>Style<\/code>-object only has a virtual <code>format<\/code> method. Note that a  generation of a <code>Style<\/code> object returns a reference to the generated object as expressed by assigning <code>this(Style)<\/code> to the return variable <code>S<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may now define <code>DefaultHeading<\/code> and <code>DefaultBodyText<\/code> as subclasses of <code>Style<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-tiny-font-size\"><code><strong>class<\/strong> DefaultHeading: Style\n   secNo: <strong>var<\/strong> integer\n   format::&lt;\n      secNo := secNo + 1\n      formattedContent := \"\\n\" + secNo + \". \" + content \n<strong>class<\/strong> DefaultBodyText: Style\n   format::&lt;\n      pos: <strong>var<\/strong> integer\n      pos := 3\n      formattedContent := \"\\n   \" \n      for (1):to(content.length):repeat\n          pos := pos + 1\n          if (pos &gt; lineMax):then\n             formattedContent := formattedContent + \"\\n   \"\n             pos := 3\n          formattedContent := formattedContent + content&#91;inx]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>DefaultHeading<\/code> has a local variable <code>secNo<\/code> keeping track of the heading numbers. The <code>format<\/code> method increments <code>secNo<\/code> by one and returns a <code>String<\/code> consisting of a newline (<code>\"\\n\"<\/code>), followed by <code>secNo<\/code> followed by a dot and a blank (<code>\". \"<\/code>) and the string hold in the <code>content<\/code> parameter of <code>format<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>format<\/code> method of class <code>DefaultBodyText<\/code> appends the characters in <code>content<\/code> to <code>formattedContent<\/code>. A newline and there blanks (<code>\"\\n   \"<\/code>) are inserted so each line has a maximum of <code>lineMax<\/code> characters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may define a standard template as a subclass of <code>Template<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code><strong>class<\/strong> StandardTemplate: Template\n   heading := DefaultHeading\n   bodyText:= DefaultBodyText\t <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>StandardTemplate<\/code> object initializes the predefined styles <code>heading<\/code> and <code>bodyText<\/code> to <code>defaultHeading<\/code> and <code>defaultBodyText<\/code> respectively.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may enclose the above classes in a class <code>SimpleTextFormatter<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code><strong>class<\/strong> SimpleTextFormatter:\n   <strong>class<\/strong> Document:\n      ...\n   <strong>class<\/strong> Template:\n      ...\n   <strong>class<\/strong> Style:\n      ...\n   <strong>class<\/strong> StandardTemplate: Template\n     ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We may now show a simple example of using the text formatter:<\/p>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>myFormatter: <strong>obj<\/strong> SimpleTextFormatter\n   doc: <strong>obj<\/strong> Document(StandardTemplate,10)\n      addCont(T: <strong>ref<\/strong> Text, S: <strong>ref<\/strong> Style):\n         addPar(Paragraph(T,S))\n   doc.addCont(\"Introduction\",temp.heading)\n   doc.addCont(\"Once upon a time, ...\",temp.bodyText)\n   doc.addCont(\"The problem\",temp.heading)\n   doc.addCont(\"There was a programmer, ...\",temp.bodyText)\n   doc.print<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The singular object <code>myFormatter<\/code> is subclassed from <code>SimpleTextFormatter<\/code>.<\/li>\n\n\n\n<li>It defines a singular object <code>doc<\/code> subclassed from <code>Document<\/code> with arguments <code>StandardTemplate<\/code> and <code>10<\/code> &#8211; the latter setting <code>lineMax<\/code> to 10 characters. Note that <code>StandardTemplate<\/code> generates an object and a reference to this object is passed as the first argument to <code>Document<\/code>.<\/li>\n\n\n\n<li>The <code>doc<\/code>-object has a method <code>addCont<\/code>, which inserts a <code>Paragraph<\/code> of a given <code>Style<\/code> in the <code>Document<\/code>.<\/li>\n\n\n\n<li>Then <code>doc.addCont<\/code> is invoked four times putting two headings and two body texts into the document.  For the headings, the <code>theStyle.bodyText<\/code> is used and for the body texts <code>theStyle.bodyText<\/code> is used.<\/li>\n\n\n\n<li>Finally it <code>prints<\/code> the document which gives the following output:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code has-cyan-bluish-gray-background-color has-background has-small-font-size\"><code>1. Introduction\n   Once up\n   on a tim\n   e, ...\n2. The problem\n   There w\n   as a pro\n   grammer,\n    ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As can be seen, the format method may insert newlines inside a word &#8211; a real text formatting system should of course not do that.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Discussion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the example above, we have only defined a single subclass, <code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\">StandardDocument<\/mark><\/code> of <code>Document<\/code> and a single <code>StandardTemplate<\/code>. We may of course defined several subclasses of <code>Document<\/code> and <code>Template<\/code>. Most publishers, professional societies, etc., have their own document and template formats. For MS Word and LaTex there are numerous possibilities depending on the use of the document.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The above text formatter is very simple. If you consider at systems like MS Word, it contains a large amount of options for formating a given document. This includes fonts and font families &#8211; class <code>Style<\/code> may be extended to include a font, a font size, etc. A <code>Paragraph<\/code> may include how it is to be adjusted. A <code>Document<\/code> may have  a size (A4, US Letter, &#8230;) and it may have a page orientation (landscape or portrait). <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In MS Word its is possible to change the template of a given document. This implies that there must be a procedure\/algorithm for converting the style of each paragraph to a similar style in the new tempalte. In MS Word this is mainly done by selecting a style of the same name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a major task to develop a text system that resembles MS Word. The above classes may be extended to some extent, but it is unlikely that the structure of these classes may expand into something like MS Word. We leave it is a challenge!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add methods that generates a HTML-document from the content of a <code>Document<\/code> object. <\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=1234\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>Here we present another example of using nested classes. The domain is text formatting as known from e.g. Microsoft Word, and LaTex. The complexity of most of these systems is huge. Here we will thus stick to a very simple text formatting system. The phenomena of our domain are documents that consists of a number [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2825,"menu_order":10,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1234","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1234"}],"version-history":[{"count":70,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1234\/revisions"}],"predecessor-version":[{"id":4802,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1234\/revisions\/4802"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2825"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}