Building a Structured Outline

1

In a fictional document management system, input documents are structured with weighted headings. Lowest weighted headings are the most important.

This is a sample document:

H1 All About Birds

H2 Kinds of Birds

H3 The Finch

H3 The Swan

H2 Habitats

H3 Wetlands

From this document we would like to produce an outline using nested ordered lists in HTML, which would look like this when rendered:

  1. All About Birds
    1. Kinds of Birds
      1. The Finch
      2. The Swan
    2. Habitats
      1. Wetlands

Your code only needs to take the input list and transform it into the expected data structure, which will be printed.

Tlink

Posted 2019-05-11T20:20:43.573

Reputation: 11

Question was closed 2019-05-11T23:02:29.277

1When you say "using nested ordered lists in HTML" do you mean we need to produce HTML? If so could you define its form for us? (Or may we just indent using a space (or a tab?)?) – Jonathan Allan – 2019-05-11T20:37:07.467

Can the titles be assumed to only contain letters and spaces? – ArBo – 2019-05-11T21:06:33.753

2

Hi and welcome to codegolf! To avoid having to answer questions like those you received, please note that we have a sandbox for proposed challenges which allows you to receive feedback before posting. As is you are missing a few important details, like those already asked about, and things like what the highest header level there will be (or if it is indefinite).

– FryAmTheEggman – 2019-05-11T21:26:39.670

This feels familiar; I think we may have had this already using Markdown's # instead of HTML's <h. – Shaggy – 2019-05-11T21:50:26.423

@Shaggy except this seems to be about using nested ordered lists <ol> rather than headings. – Nick Kennedy – 2019-05-11T22:44:34.230

1May the level be greater than $9$? – Arnauld – 2019-05-11T23:06:59.570

2Are we guaranteed to always have level $L+1$ between levels $L$ and $L+2$? Are we supposed to return valid HTML? (with all trailing </ol> tags) – Arnauld – 2019-05-11T23:35:06.433

Answers

0

Jelly, 53 50 bytes

Ḋœṡ¥€⁶ZµḢVØ0jI>0ẋ"AƲị“<ol>“</ol>”żṪ“<li>“</li>”jⱮƊ

Try it online!

A monadic link taking a list of strings as input and outputting an HTML fragment with nested ordered lists. Should handle arbitrarily deep nesting of lists and return a syntactically valid HTML fragment.

Example:

H1 All About Birds
H2 Kinds of Birds
H3 The Finch
H3 The Swan
H2 Habitats
H3 Wetlands
H4 Types of wetlands
H5 Very wet wetland
H3 Coast
H1 All about fish
H2 Sharks

yields:

<ol><li>All About Birds</li><ol><li>Kinds of Birds</li><ol><li>The Finch</li><li>The Swan</li></ol><li>Habitats</li><ol><li>Wetlands</li><ol><li>Types of wetlands</li><ol><li>Very wet wetland</li></ol></ol><li>Coast</li></ol></ol><li>All about fish</li><ol><li>Sharks</li></ol></ol>
  1. All About Birds
    1. Kinds of Birds
      1. The Finch
      2. The Swan
    2. Habitats
      1. Wetlands
        1. Types of wetlands
          1. Very wet wetland
      2. Coast
  2. All about fish
    1. Sharks

Nick Kennedy

Posted 2019-05-11T20:20:43.573

Reputation: 11 829