summaryrefslogtreecommitdiff
path: root/after/ftplugin/html.lua
blob: bfc21d489511bb0d6dfc629dfa32cc4e3e30af62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
-- html snippets 
function Snippets(snipType, value)
    snippet = ""
    if snipType == "comment" then
	snippet = [[<!-- -->]]
    elseif snipType == "tag" then
	snippet = string.format("<%s></%s>", value, value)
    elseif snipType == "linebreak" then
        snippet = [[<br>]]
    elseif snipType == "hrline" then
        snippet = [[<hr>]]
    elseif snipType == "link" then
        snippet = string.format([[<a href="%s">%s</a>]], 
            value["url"], value["label"]
        )
    elseif snipType == "pieces" then
        if value == "init" then
            snippet = [[
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" href="StyleSheetName.css"/>
</head>
<body>
</body>
</html>
            ]]
        elseif value == "paper" then
            snippet = [[
<style>
html {
    background: #ffffd7;
}
</style>
            ]]
        elseif value == "cjHeader" then
            snippet = string.format([[
<section>
    <h1>_title_</h1>
    <p>
        <i>_subtitle_</i><br>
        %s<br>
        <a href="index.html">The Cursory Journal</a>
        <hr>
    </p>
</section>
            ]], os.date("%Y/%m/%d"))
        end
    end
    return snippet
end

function JumpInTag()
    vim.cmd("normal! f>l")
end

-- keymaps to insert snippets
vim.keymap.set('i', "<M-s><M-c>", function() 
    WriteText(Snippets("comment"))
end, {noremap=true})

vim.keymap.set('i', "<M-s><M-i>", function()
    WriteText(Snippets("tag", "i"))
    JumpInTag()
end, {noremap=true})

vim.keymap.set('i', "<M-s><M-b>", function()
    WriteText(Snippets("tag", "b"))
    JumpInTag()
end, {noremap=true})

vim.keymap.set('i', "<M-s><M-/>", function()
    WriteText(Snippets("linebreak"))
end, {noremap=true})

vim.keymap.set('i', "<M-s><M-->", function()
    WriteText(Snippets("hrline"))
end, {noremap=true})

vim.keymap.set('i', "<M-s><M-t>", function()
    inp = vim.fn.input("Tag: ", "", "file")
    WriteText(Snippets("tag", inp))
    JumpInTag()
end, {remap=true})

vim.keymap.set('i', "<M-s><M-l>", function()
    url = vim.fn.input("URL: ", "", "file")
    label = vim.fn.input("label: ", "", "file")
    value = {}
    value.url = url
    value.label = label
    WriteText(
        Snippets("link", value)
    )
    JumpInTag()
end, {remap=true})


vim.keymap.set('i', "<M-s><M-d>", function()
    dt = os.date("%Y/%m/%d")
    WriteText(dt)
end)

vim.keymap.set('i', "<M-s><M-p>", function()
    name = vim.fn.input("name: ", "", "file")
    WriteLines(
        StrSplit(Snippets("pieces", name), '\n')
    )
end)

function WriteText(toWrite)
    if not toWrite then
	return
    end
    local posYX = vim.api.nvim_win_get_cursor(0)
    vim.api.nvim_buf_set_text(0, posYX[1]-1, posYX[2], posYX[1]-1, posYX[2], { toWrite })
end

function WriteLines(toWrite)
    if not toWrite then
        return
    end
    local posYX = vim.api.nvim_win_get_cursor(0)
    print(posYX[0], posYX[1], posYX[2])
    vim.api.nvim_buf_set_lines(0, posYX[1]-1, posYX[1]-1, true, toWrite)
end

function StrSplit(inpstr, sep)
    if not sep then
        sep = "%s"
    end
    local str_list = {}
    for str in string.gmatch(inpstr, "([^"..sep.."]+)") do
        table.insert(str_list, str)
    end
    return str_list
end