Skip to content

Commit

Permalink
1.9.3 Code Slide formatting tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinPacker committed Mar 21, 2021
1 parent e1c87af commit 3da6773
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 5 deletions.
112 changes: 109 additions & 3 deletions md2pptx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ try:
except:
have_cairosvg = False

md2pptx_level = "1.9.2"
md2pptx_date = "16 March, 2021"
md2pptx_level = "1.9.3"
md2pptx_date = "21 March, 2021"


class slideInfo:
Expand Down Expand Up @@ -1288,7 +1288,7 @@ def createCodeSlide(presentation, slideNumber, titleText, code):
# Fill the code box with green
fill = codeBox.fill
fill.solid()
fill.fore_color.rgb = RGBColor(223, 255, 223)
fill.fore_color.rgb = RGBColor.from_string(codeBackground)

# Get the sole paragraph
p = codeBox.text_frame.paragraphs[0]
Expand All @@ -1303,6 +1303,25 @@ def createCodeSlide(presentation, slideNumber, titleText, code):
else:
p.font.size = int(Pt(defaultBaseTextSize) / divisor)

# Estimate how wide the code box would need to be at the current font size
# versus actual width
estimatedWidthVersusCodeboxWidth = (
p.font.size * codeColumns / codeBox.width / fixedPitchHeightWidthRatio
)
if estimatedWidthVersusCodeboxWidth > 1:
# The code is wider so shrink the font so the code fits
p.font.size = p.font.size / estimatedWidthVersusCodeboxWidth
else:
# The code is narrower so shrink the code textbox so the code just fits
# - assuming declared width is accurate
codeBox.width = int(p.font.size * codeColumns / fixedPitchHeightWidthRatio)

# Center the code box
codeBox.left = int((presentation.slide_width - codeBox.width) / 2)

# Use the code foreground colour - whether explicit or defaulted
p.font.color.rgb = RGBColor.from_string(codeForeground)

# Adjust code box height based on lines
codeBox.height = min(len(codeLines) * Pt(baseTextSize + 5), contentHeight)

Expand Down Expand Up @@ -2941,6 +2960,25 @@ defaultBaseTextDecrement = 2
presBaseTextDecrement = defaultBaseTextDecrement
baseTextDecrement = defaultBaseTextDecrement

defaultCodeForeground = "000000"
presCodeForeground = defaultCodeForeground
codeForeground = defaultCodeForeground

defaultCodeBackground = "DFFFDF"
presCodeBackground = defaultCodeBackground
codeBackground = defaultCodeBackground

# Column count to work out if code should fit
defaultCodeColumns = 80
codeColumns = defaultCodeColumns
presCodeColumns = defaultCodeColumns

# Assume a fixed pitch character is this times as high as it is wide
defaultFixedPitchHeightWidthRatio = 1.2
fixedPitchHeightWidthRatio = defaultFixedPitchHeightWidthRatio
presFixedPitchHeightWidthRatio = defaultFixedPitchHeightWidthRatio


sectionTitleSize = 40
sectionSubtitleSize = 28
monoFont = "Courier"
Expand Down Expand Up @@ -3209,28 +3247,54 @@ for line in metadata_lines:

elif name == "glossarytitle":
glossaryTitle = value

elif name == "glossaryterm":
glossaryTerm = value

elif name == "glossarymeaning":
glossaryMeaning = value

elif name == "glossarymeaningwidth":
glossaryMeaningWidth = int(value)

elif name == "glossarytermsperpage":
glossaryTermsPerPage = int(value)

elif name == "footnotesperpage":
footnotesPerPage = int(value)

elif name == "footnotestitle":
footnotesTitle = value

elif name.startswith("style.bgcolor."):
spanClass = name[14:]
bgcolors[spanClass] = value

elif name.startswith("style.fgcolor."):
spanClass = name[14:]
fgcolors[spanClass] = value

elif name.startswith("style.emphasis."):
spanClass = name[15:]
emphases[spanClass] = value

elif name == "codeforeground":
codeForeground = value
presCodeForeground = value

elif name == "codebackground":
codeBackground = value
presCodeBackground = value

elif name == "fpratio":
fixedPitchHeightWidthRatio = float(value)
presFixedPitchHeightWidthRatio = float(value)

elif name == "codecolumns":
codeColumns = int(value)
presCodeColumns = int(value)


if slideTemplateFile != "":
originalSlideTemplateFile = slideTemplateFile
if Path(os.path.expanduser(slideTemplateFile)).exists():
Expand Down Expand Up @@ -3563,6 +3627,48 @@ for line in linesAfterConcatenation:
else:
cardTitlePosition = metadataValue

elif metadataKey == "codeforeground":
# Code slide foreground
if metadataValue == "default":
codeForeground = defaultCodeForeground

elif metadataValue == "pres":
codeForeground = presCodeForeground

else:
codeForeground = metadataValue

elif metadataKey == "codebackground":
# Code slide background
if metadataValue == "default":
codeBackground = defaultCodeBackground

elif metadataValue == "pres":
codeBackground = presCodeBackground

else:
codeBackground = metadataValue

elif metadataKey == "fpratio":
# Fixed Pitch font height to width ratio
if metadataValue == "default":
fixedPitchHeightWidthRatio = defaultFixedPitchHeightWidthRatio

elif metadataValue == "pres":
fixedPitchHeightWidthRatio = presFixedPitchHeightWidthRatio

else:
fixedPitchHeightWidthRatio = float(metadataValue)

elif metadataKey == "codecolumns":
# Code slide column count
if metadataValue == "default":
codeColumns = defaultCodeColumns
elif metadataValue == "pres":
codeColumns = presCodeColumns
else:
codeColumns = int(metadataValue)

else:
# Invalid dynamic metadata specification
print(f"Invalid dynamic metadata key: '{metadataKey}' in '{line}'")
Expand Down
41 changes: 41 additions & 0 deletions user-guide.html
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,11 @@ <h3 id="table-of-contents">Table Of Contents<a class="headerlink" href="#table-o
</li>
</ul>
</li>
<li><a href="#code-slide-metadata">Code Slide Metadata</a><ul>
<li><a href="#code-column-count-codecolumns">Code Column Count - <code>CodeColumns</code></a></li>
<li><a href="#fixed-pitch-height-to-width-ratio-fpratio">Fixed Pitch Height To Width Ratio - <code>FPRatio</code></a></li>
</ul>
</li>
<li><a href="#dynamic-metadata">Dynamic Metadata</a><ul>
<li><a href="#compacttables"><code>CompactTables</code></a></li>
<li><a href="#cardpercent"><code>CardPercent</code></a></li>
Expand All @@ -1118,6 +1123,8 @@ <h3 id="table-of-contents">Table Of Contents<a class="headerlink" href="#table-o
<li><a href="#pagetitlesize"><code>PageTitleSize</code></a></li>
<li><a href="#basetextsize"><code>BaseTextSize</code></a></li>
<li><a href="#basetextdecrement"><code>BaseTextDecrement</code></a></li>
<li><a href="#codecolumns"><code>CodeColumns</code></a></li>
<li><a href="#fpratio"><code>FPRatio</code></a></li>
</ul>
</li>
</ul>
Expand Down Expand Up @@ -1225,6 +1232,11 @@ <h2 id="change-log">Change Log<a class="headerlink" href="#change-log" title="Pe
</thead>
<tbody>
<tr>
<td align="left">1.9.3</td>
<td align="right">21&nbsp;March&nbsp;2021</td>
<td align="left">Added controls on how many columns wide code is and fixed pitch height to width ratio.</td>
</tr>
<tr>
<td align="left">1.9.2</td>
<td align="right">16&nbsp;March&nbsp;2021</td>
<td align="left">Added <code>&lt;pre&gt;</code>&comma; <code>&lt;code&gt;</code>&comma; triple backtick - with <code>&lt;span&gt;</code> colouring for <code>&lt;pre&gt;</code>. Added ways to make a no-title slide.</td>
Expand Down Expand Up @@ -1662,6 +1674,7 @@ <h3 id="code-slides">Code Slides<a class="headerlink" href="#code-slides" title=
</ul>
<p>When we say &ldquo;code&rdquo;, what you&rsquo;re displaying could be something like a terminal screen shot, of course. What&rsquo;s important is that it will be rendered in a fixed-pitch font.</p>
<p>In each case, the heading for the slide is generally introduced with heading level 3 - <code>###</code>. However you can start a slide without a heading. See <a href="#slides-without-titles">Slides Without Titles</a> for how to do this.</p>
<p><strong>Note:</strong> You can more precisely control how code slides are laid out with <a href="#code-slide-metadata">Code Slide Metadata</a>.</p>
<h4 id="code"><code>&lt;code&gt;</code><a class="headerlink" href="#code" title="Permanent link"></a></h4>
<p>The HTML <code>&lt;code&gt;</code> element is supported. Surround the block of text by <code>&lt;code&gt;</code> and <code>&lt;/code</code>:</p>
<pre><code>### This Is A Code Slide
Expand Down Expand Up @@ -2415,6 +2428,22 @@ <h5 id="card-shape-cardshape">Card Shape - <code>CardShape</code><a class="heade
</code></pre>
<p>The default value is <code>rounded</code>.</p>
<p>You can override this value on a slide-by-slide basis with <a href="#cardshape-dynamic">Dynamic CardShape</a>.</p>
<p><a id="code-slide-metadata"></></p>
<h3 id="code-slide-metadata">Code Slide Metadata<a class="headerlink" href="#code-slide-metadata" title="Permanent link"></a></h3>
<p>You can adjust how code slides display their content.</p>
<p><a id="code-column-count-codecolumns"></a></p>
<h4 id="code-column-count-codecolumns">Code Column Count - <code>CodeColumns</code><a class="headerlink" href="#code-column-count-codecolumns" title="Permanent link"></a></h4>
<p>You can adjust how wide the text box the code is displayed in using <code>CodeColumns</code>. For example:</p>
<pre><code>CodeColumns: 70
</code></pre>
<p>The default value is 80.</p>
<p>You can override this on a slide-by-slide basis with <a href="#dynamic-codecolumns">Dynamic CodeColumns</a>.</p>
<p><a id="fixed-pitch-height-to-width-ration-fpratio"></a></p>
<h4 id="fixed-pitch-height-to-width-ratio-fpratio">Fixed Pitch Height To Width Ratio - <code>FPRatio</code><a class="headerlink" href="#fixed-pitch-height-to-width-ratio-fpratio" title="Permanent link"></a></h4>
<p>md2pptx assumes the height of a column is 1.2 times its width. You can adjust this ratio with <code>FPRatio</code>:</p>
<pre><code>FPRatio: 1.1
</code></pre>
<p>You can override this on a slide-by-slide basis with <a href="#dynamic-fpratio">Dynamic FPRatio</a>.</p>
<p><a id="dynamic-metadata"></a></p>
<h3 id="dynamic-metadata">Dynamic Metadata<a class="headerlink" href="#dynamic-metadata" title="Permanent link"></a></h3>
<p>md2pptx can alter some in-effect settings, starting at a particular slide. Straight after the heading code a special form of comment like so:</p>
Expand Down Expand Up @@ -2478,6 +2507,18 @@ <h4 id="basetextdecrement"><code>BaseTextDecrement</code><a class="headerlink" h
<pre><code>&lt;!-- md2pptx: basetextdecrement: 0 --&gt;
</code></pre>
<p>The above stops each level of bullets having progressively smaller text.</p>
<p><a id="dynamic-codecolumns"></a></p>
<h4 id="codecolumns"><code>CodeColumns</code><a class="headerlink" href="#codecolumns" title="Permanent link"></a></h4>
<p>You can override the presentation <a href="#code-column-count-codecolumns">CodeColumns</a> metadata value. For example:</p>
<pre><code>&lt;!-- md2pptx: codecolumns: 60 --&gt;
</code></pre>
<p>The above changes the <code>CodeColumns</code> value for this and subsequent slides to 60.</p>
<p><a id="dynamic-fpratio"></a></p>
<h4 id="fpratio"><code>FPRatio</code><a class="headerlink" href="#fpratio" title="Permanent link"></a></h4>
<p>You can override the presentation <a href="#fixed-pitch-height-to-width-ration-fpratio">FPRatio</a> (fixed pitch font character height to width ratio) metadata value. For example:</p>
<pre><code>&lt;!-- md2pptx: fpratio: 1.3 --&gt;
</code></pre>
<p>The above changes the <code>FPRatio</code> value for this and subsequent slides to 1.3.</p>
<h2 id="modifying-the-slide-template">Modifying The Slide Template<a class="headerlink" href="#modifying-the-slide-template" title="Permanent link"></a></h2>
<p><a id="modifying-the-slide-template"></a></p>
<p>The included template presentation - Martin Template.pptx - is what the author tested with and gives good results. However, you probably want to develop your own template from it.</p>
Expand Down
9 changes: 7 additions & 2 deletions user-guide.log
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ mdpre Markdown Preprocessor v0.4.6 (26 January, 2021)
Def mdpre_date = 26 January, 2021
Def mdpre_level = 0.4.6
Def userid = martinpacker
Def time = 16&colon;05
Def date = 16 March&comma; 2021
Def time = 19&colon;43
Def date = 21 March&comma; 2021
Def TOC = Table Of Contents
Def md = Markdown
Def pp = Powerpoint
Expand Down Expand Up @@ -97,6 +97,9 @@ CSV Stop
..... ..... ..... ..... Card Title Alignment - `CardTitleAlign`
..... ..... ..... ..... Card Title Position - `CardTitlePosition`
..... ..... ..... ..... Card Shape - `CardShape`
..... ..... Code Slide Metadata
..... ..... ..... Code Column Count - `CodeColumns`
..... ..... ..... Fixed Pitch Height To Width Ratio - `FPRatio`
..... ..... Dynamic Metadata
..... ..... ..... `CompactTables`
..... ..... ..... `CardPercent`
Expand All @@ -107,6 +110,8 @@ CSV Stop
..... ..... ..... `PageTitleSize`
..... ..... ..... `BaseTextSize`
..... ..... ..... `BaseTextDecrement`
..... ..... ..... `CodeColumns`
..... ..... ..... `FPRatio`
..... Modifying The Slide Template
..... ..... Basics
..... ..... Slide Template Sequence
Expand Down
54 changes: 54 additions & 0 deletions user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ In this document we'll refer to it as "md2pptx", pronounced "em dee to pee pee t
* [Card Title Alignment - `CardTitleAlign`](#card-title-alignment-cardtitlealign)
* [Card Title Position - `CardTitlePosition`](#card-title-position-cardtitleposition)
* [Card Shape - `CardShape`](#card-shape-cardshape)
* [Code Slide Metadata](#code-slide-metadata)
* [Code Column Count - `CodeColumns`](#code-column-count-codecolumns)
* [Fixed Pitch Height To Width Ratio - `FPRatio`](#fixed-pitch-height-to-width-ratio-fpratio)
* [Dynamic Metadata](#dynamic-metadata)
* [`CompactTables`](#compacttables)
* [`CardPercent`](#cardpercent)
Expand All @@ -94,6 +97,8 @@ In this document we'll refer to it as "md2pptx", pronounced "em dee to pee pee t
* [`PageTitleSize`](#pagetitlesize)
* [`BaseTextSize`](#basetextsize)
* [`BaseTextDecrement`](#basetextdecrement)
* [`CodeColumns`](#codecolumns)
* [`FPRatio`](#fpratio)
* [Modifying The Slide Template](#modifying-the-slide-template)
* [Basics](#basics)
* [Slide Template Sequence](#slide-template-sequence)
Expand Down Expand Up @@ -205,6 +210,7 @@ To quote from the python-pptx license statement:

|Level|Date|What|
|:-|-:|:-|
|1.9.3|21&nbsp;March&nbsp;2021|Added controls on how many columns wide code is and fixed pitch height to width ratio.|
|1.9.2|16&nbsp;March&nbsp;2021|Added `<pre>`&comma; `<code>`&comma; triple backtick - with `<span>` colouring for `<pre>`. Added ways to make a no-title slide.|
|1.9.1|6&nbsp;March&nbsp;2021|Added `&check;` entity reference. Reworked internals with more consistent layout and `getContentRec` and title formatting improvements. Prereq Python 3.8.|
|1.9|17&nbsp;February&nbsp;2021|Add support to specify which slide in master. Also numbersMargin|
Expand Down Expand Up @@ -598,6 +604,8 @@ When we say "code", what you're displaying could be something like a terminal sc

In each case, the heading for the slide is generally introduced with heading level 3 - `### `. However you can start a slide without a heading. See [Slides Without Titles](#slides-without-titles) for how to do this.

**Note:** You can more precisely control how code slides are laid out with [Code Slide Metadata](#code-slide-metadata).

#### `<code>`

The HTML `<code>` element is supported. Surround the block of text by `<code>` and `</code`:
Expand Down Expand Up @@ -1453,6 +1461,32 @@ The default value is `rounded`.

You can override this value on a slide-by-slide basis with [Dynamic CardShape](#cardshape-dynamic).

<a id="code-slide-metadata"></>
### Code Slide Metadata

You can adjust how code slides display their content.

<a id="code-column-count-codecolumns"></a>
#### Code Column Count - `CodeColumns`

You can adjust how wide the text box the code is displayed in using `CodeColumns`. For example:

CodeColumns: 70

The default value is 80.


You can override this on a slide-by-slide basis with [Dynamic CodeColumns](#dynamic-codecolumns).

<a id="fixed-pitch-height-to-width-ration-fpratio"></a>
#### Fixed Pitch Height To Width Ratio - `FPRatio`

md2pptx assumes the height of a column is 1.2 times its width. You can adjust this ratio with `FPRatio`:

FPRatio: 1.1

You can override this on a slide-by-slide basis with [Dynamic FPRatio](#dynamic-fpratio).

<a id="dynamic-metadata"></a>
### Dynamic Metadata

Expand Down Expand Up @@ -1543,6 +1577,26 @@ You can override the presentation [BaseTextDecrement](#specifying-text-size-with

The above stops each level of bullets having progressively smaller text.

<a id="dynamic-codecolumns"></a>

#### `CodeColumns`

You can override the presentation [CodeColumns](#code-column-count-codecolumns) metadata value. For example:

<!-- md2pptx: codecolumns: 60 -->

The above changes the `CodeColumns` value for this and subsequent slides to 60.

<a id="dynamic-fpratio"></a>

#### `FPRatio`

You can override the presentation [FPRatio](#fixed-pitch-height-to-width-ration-fpratio) (fixed pitch font character height to width ratio) metadata value. For example:

<!-- md2pptx: fpratio: 1.3 -->

The above changes the `FPRatio` value for this and subsequent slides to 1.3.

## Modifying The Slide Template
<a id="modifying-the-slide-template"></a>

Expand Down

0 comments on commit 3da6773

Please sign in to comment.