<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.luatex.org/index.php?action=history&amp;feed=atom&amp;title=T_wo_T_r%3Amain.lua</id>
	<title>T wo T r:main.lua - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.luatex.org/index.php?action=history&amp;feed=atom&amp;title=T_wo_T_r%3Amain.lua"/>
	<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=T_wo_T_r:main.lua&amp;action=history"/>
	<updated>2026-05-31T15:12:50Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.1</generator>
	<entry>
		<id>https://wiki.luatex.org/index.php?title=T_wo_T_r:main.lua&amp;diff=3561&amp;oldid=prev</id>
		<title>Rkrug: Created page with &quot; &lt;nowiki&gt;   --- Copyright (c) 2021 by Toadstone Enterprises. --- ISC-type license, see License.txt for details.   -------------------------------------------------------------...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.luatex.org/index.php?title=T_wo_T_r:main.lua&amp;diff=3561&amp;oldid=prev"/>
		<updated>2021-02-22T18:32:47Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; &amp;lt;nowiki&amp;gt;   --- Copyright (c) 2021 by Toadstone Enterprises. --- ISC-type license, see License.txt for details.   -------------------------------------------------------------...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt; &amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--- Copyright (c) 2021 by Toadstone Enterprises.&lt;br /&gt;
--- ISC-type license, see License.txt for details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-----------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
--- Why is this needed?&lt;br /&gt;
local main = {}&lt;br /&gt;
package.loaded[...] = main&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local util = require(&amp;quot;utils&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
local link_nodes     = util.link_nodes&lt;br /&gt;
local make_glyph     = util.make_glyph&lt;br /&gt;
local make_glue      = util.make_glue&lt;br /&gt;
local make_penalty   = util.make_penalty&lt;br /&gt;
local is_whitespace  = util.is_whitespace&lt;br /&gt;
local is_linefeed    = util.is_linefeed&lt;br /&gt;
local is_nobreak     = util.is_nobreak&lt;br /&gt;
local copy_table     = util.copy_table&lt;br /&gt;
local read_line      = util.read_line&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local reader = require(&amp;quot;reader&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
local read_value   = reader.read_value&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local format = require(&amp;quot;format&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
local update_state         = format.update_state&lt;br /&gt;
local update_locals        = format.update_locals&lt;br /&gt;
local push_tbl             = format.push_tbl&lt;br /&gt;
local pop_tbl              = format.pop_tbl&lt;br /&gt;
local top_tbl              = format.top_tbl&lt;br /&gt;
local pop_do_command_stop  = format.pop_do_command_stop&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local commands = commands or require(&amp;quot;commands&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
local get_command        = commands.get_command&lt;br /&gt;
local initialize_command = commands.initialize_command&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local pages = pages or require(&amp;quot;pages&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
local build_par   = pages.build_par&lt;br /&gt;
local process_par = pages.process_par&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-----------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local do_char = function(value, head, tail)&lt;br /&gt;
  -- We are building the hlist that will be passed on to&lt;br /&gt;
  -- tex.linebreak for creating a paragraph.&lt;br /&gt;
&lt;br /&gt;
  local tbl = top_tbl()&lt;br /&gt;
&lt;br /&gt;
  local n = nil&lt;br /&gt;
  local p = nil  -- for penalties&lt;br /&gt;
&lt;br /&gt;
  if is_nobreak(value) then&lt;br /&gt;
    -- we treat all unbreakable whitespace equally&lt;br /&gt;
    p = make_penalty(10000)&lt;br /&gt;
    n = make_glue(&amp;quot;spaceskip&amp;quot;,&lt;br /&gt;
                  tbl.space,&lt;br /&gt;
                  tbl.space_stretch,&lt;br /&gt;
                  tbl.space_shrink)&lt;br /&gt;
  elseif is_whitespace(value) then&lt;br /&gt;
    -- we treat all (breakable) whitespace equally&lt;br /&gt;
    n = make_glue(&amp;quot;spaceskip&amp;quot;,&lt;br /&gt;
                  tbl.space,&lt;br /&gt;
                  tbl.space_stretch,&lt;br /&gt;
                  tbl.space_shrink)&lt;br /&gt;
  else&lt;br /&gt;
    -- presumably a glyph&lt;br /&gt;
    n = make_glyph(value,&lt;br /&gt;
                   tbl.font,&lt;br /&gt;
                   tbl.lang,&lt;br /&gt;
                   tbl.lefthyphenmin,&lt;br /&gt;
                   tbl.righthyphenmin)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  if (head == nil) then&lt;br /&gt;
    -- starting a new paragraph, but we add the initial glue in&lt;br /&gt;
    -- build_par.&lt;br /&gt;
    head = n&lt;br /&gt;
  elseif p then&lt;br /&gt;
    -- We have a penalty&lt;br /&gt;
    link_nodes(tail, p, n)&lt;br /&gt;
  else&lt;br /&gt;
    -- only a single node&lt;br /&gt;
    link_nodes(tail, n)&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  -- n is our new tail&lt;br /&gt;
  return head, n&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
local reading_par = function(head, tail)&lt;br /&gt;
  -- before we start reading a paragraph, head and tail will be nil.&lt;br /&gt;
  return (node.is_node(head) and node.is_node(tail))&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
main.main_loop = function()&lt;br /&gt;
&lt;br /&gt;
  local head, tail, eat_the_white, eol_seen = update_locals()&lt;br /&gt;
&lt;br /&gt;
  local value = read_value()&lt;br /&gt;
&lt;br /&gt;
  while value do&lt;br /&gt;
&lt;br /&gt;
    if value == unicode.utf8.byte(&amp;quot;%&amp;quot;) then&lt;br /&gt;
      -- Start of comment. Comment goes to the end of the line.&lt;br /&gt;
&lt;br /&gt;
      read_line()&lt;br /&gt;
 &lt;br /&gt;
    elseif is_linefeed(value) then&lt;br /&gt;
    &lt;br /&gt;
      if (eol_seen and reading_par(head, tail)) then&lt;br /&gt;
        -- eol_seen ==&amp;gt; this is a blank line,&lt;br /&gt;
        -- reading_par(head, tail) ==&amp;gt; we are in the middle of a paragraph.&lt;br /&gt;
        -- Thus we avoid building &amp;#039;empty&amp;#039; paragraphs.&lt;br /&gt;
        local par = build_par(head, tail)&lt;br /&gt;
        process_par(par)&lt;br /&gt;
        -- After we process the par, we reset head and tail to nil&lt;br /&gt;
        -- in preparation for the next paragraph to be read in.&lt;br /&gt;
        head = nil&lt;br /&gt;
        tail = nil&lt;br /&gt;
        &lt;br /&gt;
      elseif ((not eat_the_white) and reading_par(head, tail)) then&lt;br /&gt;
        -- (not eat_the_white) ==&amp;gt; this is the first bit of white space&lt;br /&gt;
        -- seen,&lt;br /&gt;
        -- reading_par(head, tail) ==&amp;gt; we are in the middle of a paragraph.&lt;br /&gt;
        -- Thus, we avoid starting a paragraph with white space.&lt;br /&gt;
        head, tail    = do_char(value, head, tail)&lt;br /&gt;
        eat_the_white = true&lt;br /&gt;
        eol_seen      = true&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
    elseif is_whitespace(value) then&lt;br /&gt;
    &lt;br /&gt;
      if ((not eat_the_white) and reading_par(head, tail)) then&lt;br /&gt;
        -- (not eat_the_white) ==&amp;gt; this is the first bit of white space&lt;br /&gt;
        -- seen,&lt;br /&gt;
        -- reading_par(head, tail) ==&amp;gt; we are in the middle of a paragraph.&lt;br /&gt;
        -- Thus, we avoid starting a paragraph with white space.&lt;br /&gt;
        head, tail    = do_char(value, head, tail)&lt;br /&gt;
        -- But we do not set eol_seen to false. Thus an &amp;quot;empty&amp;quot; line can&lt;br /&gt;
        -- contain white space.&lt;br /&gt;
        eat_the_white = true&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
    elseif value == unicode.utf8.byte(&amp;quot;\\&amp;quot;) then&lt;br /&gt;
    &lt;br /&gt;
      -- get the command, and initialize it&lt;br /&gt;
      command       = get_command()&lt;br /&gt;
      update_state(head, tail, false, false)&lt;br /&gt;
      initialize_command(command)&lt;br /&gt;
      -- initialize_command may hace done some stuff, resetting&lt;br /&gt;
      -- head, tail, eat_the_white, and eol_seen so we reset them here.&lt;br /&gt;
      head, tail, eat_the_white, eol_seen = update_locals()&lt;br /&gt;
      &lt;br /&gt;
    elseif value == unicode.utf8.byte(&amp;quot;{&amp;quot;) then&lt;br /&gt;
      -- enter a group or start a command&lt;br /&gt;
      &lt;br /&gt;
      update_state(head, tail, false , false)&lt;br /&gt;
      local do_command_start = State.do_command_start&lt;br /&gt;
      if do_command_start then&lt;br /&gt;
        -- We have a command, but before executing it, we reset&lt;br /&gt;
        -- State.do_command_start so that we do not get into a loop.&lt;br /&gt;
        State.do_command_start = nil&lt;br /&gt;
        do_command_start()&lt;br /&gt;
        head, tail, eat_the_white, eol_seen = update_locals()&lt;br /&gt;
        &lt;br /&gt;
      else&lt;br /&gt;
        -- enter a group.&lt;br /&gt;
        push_tbl(copy_table(top_tbl()))&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
    elseif value == unicode.utf8.byte(&amp;quot;}&amp;quot;) then&lt;br /&gt;
      -- leave a group or stop a command&lt;br /&gt;
&lt;br /&gt;
      local do_command_stop = pop_do_command_stop()&lt;br /&gt;
      if do_command_stop then&lt;br /&gt;
        update_state(head, tail, false, false)&lt;br /&gt;
        do_command_stop()&lt;br /&gt;
        head, tail, eat_the_white, eol_seen = update_locals()&lt;br /&gt;
      else&lt;br /&gt;
        pop_tbl()&lt;br /&gt;
      end&lt;br /&gt;
      &lt;br /&gt;
    else&lt;br /&gt;
      -- presumably a glyph&lt;br /&gt;
&lt;br /&gt;
      eat_the_white = false&lt;br /&gt;
      eol_seen      = false&lt;br /&gt;
      head, tail    = do_char(value, head, tail)&lt;br /&gt;
    end&lt;br /&gt;
    &lt;br /&gt;
    value = read_value()&lt;br /&gt;
    &lt;br /&gt;
  end  -- while (value) do&lt;br /&gt;
  &lt;br /&gt;
  if reading_par(head, tail) then&lt;br /&gt;
    -- eof, and we may have a par to build.&lt;br /&gt;
    if reading_par(head, tail) then&lt;br /&gt;
      local par = build_par(head, tail)&lt;br /&gt;
      process_par(par)&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  update_state(nil, nil, false, false)&lt;br /&gt;
&lt;br /&gt;
  -- Check for unpopped tbls or do_command_stops?&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
return main&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;&lt;/div&gt;</summary>
		<author><name>Rkrug</name></author>
		
	</entry>
</feed>