Skip to content

Commit 098407f

Browse files
committed
- [+] implement excluding control. close #6
1 parent 51354af commit 098407f

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

html2md_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ const (
1212

1313
boldText = "<strong>Bold Text</strong>"
1414
boldEscape = "<strong>option src_ip</strong>"
15+
16+
excludingControlTest = `<div class="outter-class">
17+
<h1 class="inner-class">
18+
<div class="other-class3">
19+
<h3>Some heading i don't need</h3>
20+
</div>
21+
The string I need
22+
<span class="other-class" >Some value I don't need</span>
23+
<span class="other-class2" title="sometitle"></span>
24+
</h1>
25+
</div>`
1526
)
1627

1728
type testCase struct {
@@ -45,6 +56,20 @@ func TestExec(t *testing.T) {
4556
"Only <del>blue ones</del> <s> left</s>",
4657
[]string{"-i", "--plugin-strikethrough"},
4758
},
59+
{
60+
"ExclChildren", "The string I need", excludingControlTest,
61+
[]string{"-s", "h1", "--xc", "-i"},
62+
},
63+
{
64+
"Excl1", `### Some heading i don't need
65+
66+
The string I need`, excludingControlTest,
67+
[]string{"-s", "h1", "-xspan", "-i"},
68+
},
69+
{
70+
"Excl2", "The string I need", excludingControlTest,
71+
[]string{"-s", "h1", "-x", "span", "-xdiv", "-i"},
72+
},
4873
// {
4974
// "", "", "", []string{"-i"},
5075
// },

prop_html2md.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ func Html2md(ctx *cli.Context) error {
3535
doc, err := goquery.NewDocumentFromReader(rootArgv.Filei)
3636
clis.AbortOn("Reading file with goquery", err)
3737
content := doc.Find(rootArgv.Sel)
38+
if rootArgv.ExclChildren {
39+
content = content.Children().Remove().End()
40+
// h, _ := goquery.OuterHtml(content)
41+
// clis.Verbose(3, "%#v\n", h)
42+
} else if len(rootArgv.Excl) != 0 {
43+
for _, ex := range rootArgv.Excl {
44+
content = content.Find(ex).Remove().End()
45+
h, _ := content.Html()
46+
clis.Verbose(5, "%#v\n", h)
47+
}
48+
}
3849

3950
domain, url := rootArgv.Domain, rootArgv.Filei.Name()
4051
if domain == "" && regexp.MustCompile(`(?i)^http`).MatchString(url) {

0 commit comments

Comments
 (0)