0

We want to render text that looks like this:

1. Introduction. Here is a named section.

This section has a second paragraph.

2. This one does not have a name. It is logically a part of the Introduction.

3. The second section. This one does have a name.

4. This (unnamed) section is part of a group beginning with “The second section”.

5. This one is too.

The ideal markup for this example would be

<section>
  <h1>Introduction</h1>
  <section>
    <p>Here is a named section.</p>
    <p>This section has a second paragraph.</p>
  </section>
  <section><p>This one does not have a name. It is logically a part of the introduction.</p></section>
</section>
<section>
  <h1>The second section</h1>
  <section><p>This one does have a name.</p></section>
  <section><p>This (unnamed) section is part of a group beginning with “The second section”.</p></section>
  <section><p>This one is too.</p></section>
</section>

I don't want to put the section numbers in the markup; in other words, it would be best if a CSS counter were used. And indeed simply getting section numbers is easy, although making them run into the text as above is tricky.

You might be wondering what this is all about, and why I'm not using an order list. The answer is that the ultimate purpose is to translate literate programs written by Donald Knuth and others into HTML from WEB (and perhaps eventually CWEB) input. Here is an example of a WEB program, as formatted, in the usual manner, by TeX: http://mirrors.ctan.org/info/knuth-pdf/web/tangle.pdf. A WEB program is conceptually a bunch of sections that are rearranged into machine-readable code to be compiled. Thus <section> seems most appropriate for the markup. (If, however, what I'm trying to do is super easy with <ol>, then I might pivot.)

In conclusion: How might the markup be styled to create the desired formatting?

texdr.aft
  • 274
  • 2
  • 10
  • 1
    Are you expecting CSS to add the missing text? "Blah blah blah blah blah." after "Here is a named section" (also changing the horizontal ellipsis to a period), etc. If it's just about the numbers, I'd remove the extra text in your example rendering (or add it to the HTML). – Heretic Monkey Jun 09 '21 at 22:26
  • @HereticMonkey Fair point; I've changed it now. – texdr.aft Jun 09 '21 at 22:31

1 Answers1

1

Something like this?

Setting a counter on h1 and the sections that aren't following a h1.

Adding inline-block to the h1 and the section that follows it to make them share the same row.

EDIT: UPDATED WITH NEW CSS

   /*** NUMBERING ***/

body {
  counter-reset: increment;
}

/* ignore all paragraphs in first section following h1 */
section > h1 + section > p::before {
  counter-increment: none;
  content: none;
}

h1::before,
section > section > p::before {
  counter-increment: increment;
  content: counter(increment) ". ";
  font-size: 1rem;
  font-weight: bold;
}

    /*** SHARING THE SAME ROW ***/

h1 {
  display: inline-block;
  font-size: 1rem;
  margin: 0px;
}

section > h1 + section {
  display: inline;
}

section > h1 + section > p:first-child {
  display: inline;
}
<section>
  <h1>Introduction</h1>
  <section>
    <p>Here is a named section.</p>
    <p>This section has a second paragraph.</p>
  </section>
  <section>
    <p>This one does not have a name. It is logically a part of the introduction.</p>
  </section>
</section>
<section>
  <h1>The second section</h1>
  <section>
    <p>This one does have a name.</p>
  </section>
  <section>
    <p>This (unnamed) section is part of a group beginning with “The second section”.</p>
  </section>
  <section>
    <p>This one is too.</p>
  </section>
</section>

EDIT: OLD ANSWER

body {
  counter-reset: increment;
}

section > h1::before,
section > section ~ section::before {
  counter-increment: increment;
  content: counter(increment) ". ";
  font-size: 1rem;
  font-weight: bold;
}

h1 {
  display: inline-block;
  font-size: 1rem;
  margin: 0px;
}

h1 + section {
  display: inline-block;
}

section > section {
  margin-bottom: 1rem;
}
<section>
  <h1>Introduction</h1>
  <section>Here is a named section…</section>
  <section>This one does not have a name…</section>
</section>
<section>
  <h1>The second section</h1>
  <section>This one does have a name.</section>
  <section>This (unnamed) section…</section>
  <section>This one is too.</section>
</section>
Rickard Elimää
  • 7,107
  • 3
  • 14
  • 30
  • This works beautifully, except that there appear to be problems when the `
    ` contains multiple paragraphs (i.e., `

    ` elements) and further `

    `s (although I didn't require this in the question): The "heading" runs into only the last paragraph, and all other paragraphs are offset from the margin by the width of the "heading".

    – texdr.aft Jun 09 '21 at 21:44
  • I've revised the question so that the requirement of paragraphs is expressed. I think that this will require changes to this answer as well; sorry. – texdr.aft Jun 09 '21 at 22:15
  • 1
    In my opinion, you changed it to faulty semantics. If `h1` and the next paragraph should be on the same row, they should be in a section of their own (or in no section at all). – Rickard Elimää Jun 10 '21 at 18:12