Commit 155d7d81 authored by Marco Cavalli's avatar Marco Cavalli
Browse files

feat: add style for note / examples

parent 4f488d2b
Loading
Loading
Loading
Loading
Loading
+80 −0
Original line number Diff line number Diff line
@@ -198,6 +198,34 @@ function Linking(el)
    return el
end

function RemoveAfterFirstTab(text)
    -- Find the position of the first occurrence of '\t'
    local firstTabPos = text:find("\t")

    -- Check if a tab was found
    if firstTabPos then
        -- Return everything before the first '\t' as a Pandoc string
        return text:sub(1, firstTabPos - 1)
    else
        -- If there are no tabs, return the entire original string as a Pandoc string
        return text
    end
end

function RemoveBeforeLastTab(text)
    -- Find the position of the last occurrence of '\t'
    local lastTabPos = text:match(".*()\t")

    -- Check if a tab was found
    if lastTabPos then
        -- Return everything after the last '\t' as a Pandoc string
        return text:sub(lastTabPos + 1)
    else
        -- If there are no tabs, return the entire original string as a Pandoc string
        return text
    end
end

--Pandoc filters
if FORMAT:match 'html' then
    function Div(el)
@@ -213,6 +241,58 @@ if FORMAT:match 'html' then
        if el.classes[1] ~= "TH" then
            el.content = Substitute(el, "table")
        end
        if el.classes[1] == "EX" then
            if el.content[1] and el.content[1].t == "Str" then
                local firstPart = pandoc.List({})
                local secondPart = pandoc.List({})
                -- check if the tab is in the first element, e.g. "EXAMPLE:\t"
                if el.content[1].text:find("EXAMPLE:\t") or el.content[1].text:find("Example:\t") then
                    firstPart:insert(pandoc.Str("EXAMPLE:"))
                    secondPart = pandoc.List(el.content)
                    secondPart[1] = pandoc.Str(RemoveBeforeLastTab(el.content[1].text))
                -- the tab is in the third element, e.g. "EXAMPLE 1:\t"
                elseif el.content[1].text:find("EXAMPLE") then
                    secondPart = pandoc.List(el.content)
                    secondPart:remove(1)
                    secondPart:remove(1)
                    firstPart:insert(pandoc.Str("EXAMPLE " .. RemoveAfterFirstTab(secondPart[1].text)))
                    secondPart[1] = pandoc.Str(RemoveBeforeLastTab(secondPart[1].text))
                end
                --add two divs, one for the first part and one for the second
                local firstDiv = pandoc.Div(firstPart)
                firstDiv.classes = pandoc.List({})
                local secondDiv = pandoc.Div(secondPart)
                secondDiv.classes = pandoc.List({})
                --set the content of the original div to be the two new divs
                el.content = pandoc.List({firstDiv, secondDiv})
            end
        end
        if el.classes[1] == "NO" or el.classes[1] == "TAN" then
            if el.content[1] and el.content[1].t == "Str" then
                local firstPart = pandoc.List({})
                local secondPart = pandoc.List({})
                -- check if the tab is in the first element, e.g. "NOTE:\t"
                if el.content[1].text:find("NOTE:\t") then
                    firstPart:insert(pandoc.Str("NOTE:"))
                    secondPart = pandoc.List(el.content)
                    secondPart[1] = pandoc.Str(RemoveBeforeLastTab(el.content[1].text))
                -- the tab is in the third element, e.g. "NOTE 1:\t"
                elseif el.content[1].text:find("NOTE") then
                    secondPart = pandoc.List(el.content)
                    secondPart:remove(1)
                    secondPart:remove(1)
                    firstPart:insert(pandoc.Str("NOTE " .. RemoveAfterFirstTab(secondPart[1].text)))
                    secondPart[1] = pandoc.Str(RemoveBeforeLastTab(secondPart[1].text))
                end
                --add two divs, one for the first part and one for the second
                local firstDiv = pandoc.Div(firstPart)
                firstDiv.classes = pandoc.List({})
                local secondDiv = pandoc.Div(secondPart)
                secondDiv.classes = pandoc.List({})
                --set the content of the original div to be the two new divs
                el.content = pandoc.List({firstDiv, secondDiv})
            end
        end
        el.content = Substitute(el, "annex")
        return el        
    end
+17 −5
Original line number Diff line number Diff line
@@ -98,6 +98,9 @@ class CSS_Style:
            self.display = None
            self.gap = None
            self.align_items = None
        elif self.class_name == "EX" or self.class_name == "NO" or self.class_name == "TAN":
            self.display = "flex"
            self.gap = Pt(4)
    
    def inherit_property(self, style, property_path):
        """
@@ -339,6 +342,11 @@ def mark_table(tables):
                mark_style(cell.paragraphs)
                mark_table(cell.tables)
                
def redefine_tags(paragraphs):
    for para in paragraphs:
        for run in para.runs:
            para_name = get_name(para.style.name)


if __name__ == "__main__": 
    if len(sys.argv) != 2:
@@ -357,12 +365,16 @@ if __name__ == "__main__":
    #mark paragraphs and runs style info
    mark_style(doc.paragraphs)
    
    redefine_tags(doc.paragraphs)
    
    #add some custom styles
    api_css.get_para("HTML_Code").font_size = Pt(12)
    api_css.get_para("HTML_Keyboard").font_size = Pt(12)
    api_css.get_para("HTML_Sample").font_size = Pt(12)
    api_css.get_para("HTML_Variable").font_size = Pt(12)
    api_css.get_para("PL").font_size = Pt(12)
    api_css.get_para("HTML_Code").font_size = Pt(8)
    api_css.get_para("HTML_Keyboard").font_size = Pt(10)
    api_css.get_para("HTML_Sample").font_size = Pt(8)
    api_css.get_para("HTML_Variable").font_size = Pt(8)
    # api_css.get_para("PL").font_size = Pt(12)
    # api_css.get_para("TAL").font_size = Pt(12)
    # api_css.get_para("TAN").font_size = Pt(12)
                
    #save files
    api_css.write()
+12 −1
Original line number Diff line number Diff line
@@ -473,7 +473,7 @@ th {
}
body {
  font-family: -apple-system, BlinkMacSystemFont,"Times New Roman", "Segoe UI", Roboto, "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  font-size: 12pt;
  font-size: 10pt;
  line-height: 1.6;
  /* margin: auto; */
  /* max-width: 920px;
@@ -839,3 +839,14 @@ code > span.in { color: #969896; font-weight: bold; font-style: italic; } /* Inf
 /* li p {
    background-color: rgb(255,0,0);
} */



/* CUSTOM CSS
========================================================================== */

.NO > div:first-child,
.EX > div:first-child,
.TAN > div:first-child {
  text-wrap: nowrap;
}
 No newline at end of file
+12 −1
Original line number Diff line number Diff line
@@ -472,7 +472,7 @@ html {
  }
  body {
    font-family: -apple-system, BlinkMacSystemFont,"Times New Roman", "Segoe UI", Roboto, "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
    font-size: 12pt;
    font-size: 10pt;
    line-height: 1.6;
    /* margin: auto; */
    /* max-width: 920px;
@@ -839,3 +839,14 @@ html {
      background-color: rgb(255,0,0);
  } */



/* CUSTOM CSS
========================================================================== */

.NO > div:first-child,
.EX > div:first-child,
.TAN > div:first-child {
  text-wrap: nowrap;
}
  
 No newline at end of file