<commit_before>local gridSpacing -- Should be a setting

local makeUp = function ()
  local toadd = gridSpacing - (SILE.typesetter.frame.state.totals.gridCursor % gridSpacing)   
  SILE.typesetter.frame.state.totals.gridCursor = SILE.typesetter.frame.state.totals.gridCursor + toadd
  return SILE.nodefactory.newVglue({ height = SILE.length.new({ length = toadd }) })
end

local leadingFor = function(this, vbox, previous)
  if not this.frame.state.totals.gridCursor then this.frame.state.totals.gridCursor = SILE.typesetter.frame.state.cursorY end
  this.frame.state.totals.gridCursor = this.frame.state.totals.gridCursor + previous.height.length + previous.depth.length
  return makeUp()
end

local pushVglue = function(this, spec)
  if not this.frame.state.totals.gridCursor then this.frame.state.totals.gridCursor = SILE.typesetter.frame.state.cursorY end
  this.frame.state.totals.gridCursor = this.frame.state.totals.gridCursor + spec.height.length
  SILE.defaultTypesetter.pushVglue(this, spec);
  SILE.defaultTypesetter.pushVglue(this, makeUp())
end

local newBoxup = function (this)
  local b = SILE.defaultTypesetter.boxUpNodes(this)
  if #b > 1 then
    this.frame.state.totals.gridCursor = this.frame.state.totals.gridCursor + b[#b].height.length + b[#b].depth.length
  end
  return b
end

local debugGrid = function()
  local t = SILE.typesetter
  if not t.frame.state.totals.gridCursor then t.frame.state.totals.gridCursor = SILE.typesetter.frame.state.cursorY end
  local g = t.frame.state.totals.gridCursor
  while g < t.frame:bottom() do
    SILE.outputter.rule(t.frame:left(), g, t.frame:width(), 0.1)
    g = g + gridSpacing
  end
end

SILE.registerCommand("grid", function(options, content)
  SU.required(options, "spacing", "grid package")
  gridSpacing = SILE.parseComplexFrameDimension(options.spacing,"h");
  SILE.typesetter:chuck()

  SILE.typesetter.leadingFor = leadingFor
  SILE.typesetter.pushVglue = pushVglue
  SILE.typesetter.boxUpNodes = newBoxup
  SILE.typesetter.setVerticalGlue = function () end
  SILE.typesetter.frame.state.totals.gridCursor = SILE.typesetter.frame.state.cursorY
  -- add some now
  SILE.defaultTypesetter.pushVglue(SILE.typesetter, makeUp())
end, "Begins typesetting on a grid spaced at <spacing> intervals.")

SILE.registerCommand("no-grid", function (options, content)
  SILE.typesetter.leadingFor = SILE.defaultTypesetter.leadingFor
  SILE.typesetter.pushVglue = SILE.defaultTypesetter.pushVglue
  SILE.typesetter.setVerticalGlue = SILE.defaultTypesetter.setVerticalGlue
  -- SILE.typesetter.state = t.state
end, "Stops grid typesetting.")<commit_msg>More fixes.<commit_after>local gridSpacing -- Should be a setting

local makeUp = function ()
  local toadd = gridSpacing - (SILE.typesetter.frame.state.totals.gridCursor % gridSpacing)   
  SILE.typesetter.frame.state.totals.gridCursor = SILE.typesetter.frame.state.totals.gridCursor + toadd
  return SILE.nodefactory.newVglue({ height = SILE.length.new({ length = toadd }) })
end

local leadingFor = function(this, vbox, previous)
  if not this.frame.state.totals.gridCursor then this.frame.state.totals.gridCursor = SILE.typesetter.frame.state.cursorY end
  this.frame.state.totals.gridCursor = this.frame.state.totals.gridCursor + previous.height.length + previous.depth.length
  return makeUp()
end

local pushVglue = function(this, spec)
  if not this.frame.state.totals.gridCursor then this.frame.state.totals.gridCursor = SILE.typesetter.frame.state.cursorY end
  this.frame.state.totals.gridCursor = this.frame.state.totals.gridCursor + spec.height.length
  SILE.defaultTypesetter.pushVglue(this, spec);
  SILE.defaultTypesetter.pushVglue(this, makeUp())
end

local newBoxup = function (this)
  local b = SILE.defaultTypesetter.boxUpNodes(this)
  if not this.frame.state.totals.gridCursor then this.frame.state.totals.gridCursor = SILE.typesetter.frame.state.cursorY end
  
  if #b > 1 then
    this.frame.state.totals.gridCursor = this.frame.state.totals.gridCursor + b[#b].height.length + b[#b].depth.length
  end
  return b
end

local debugGrid = function()
  local t = SILE.typesetter
  if not t.frame.state.totals.gridCursor then t.frame.state.totals.gridCursor = SILE.typesetter.frame.state.cursorY end
  local g = t.frame.state.totals.gridCursor
  while g < t.frame:bottom() do
    SILE.outputter.rule(t.frame:left(), g, t.frame:width(), 0.1)
    g = g + gridSpacing
  end
end

SILE.registerCommand("grid:debug", debugGrid)

SILE.registerCommand("grid", function(options, content)
  SU.required(options, "spacing", "grid package")
  gridSpacing = SILE.parseComplexFrameDimension(options.spacing,"h");
  SILE.typesetter:chuck()

  SILE.typesetter.leadingFor = leadingFor
  SILE.typesetter.pushVglue = pushVglue
  SILE.typesetter.boxUpNodes = newBoxup
  SILE.typesetter.setVerticalGlue = function () end
  SILE.typesetter.frame.state.totals.gridCursor = SILE.typesetter.frame.state.cursorY
  -- add some now
  SILE.defaultTypesetter.pushVglue(SILE.typesetter, makeUp())
end, "Begins typesetting on a grid spaced at <spacing> intervals.")

SILE.registerCommand("no-grid", function (options, content)
  SILE.typesetter.leadingFor = SILE.defaultTypesetter.leadingFor
  SILE.typesetter.pushVglue = SILE.defaultTypesetter.pushVglue
  SILE.typesetter.setVerticalGlue = SILE.defaultTypesetter.setVerticalGlue
  -- SILE.typesetter.state = t.state
end, "Stops grid typesetting.")
{'fraction_non_alphanumeric': 0.11661104931405265, 'fraction_numerical': 0.0011123470522803114, 'mean_word_length': 4.73936170212766, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2082343', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1821, 'n_words': 333}