Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F10882668
posts.go
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Subscribers
None
posts.go
View Options
package
parse
import
(
"github.com/writeas/web-core/stringmanip"
"regexp"
"strings"
)
var
(
titleElementReg
=
regexp
.
MustCompile
(
"</?p>"
)
urlReg
=
regexp
.
MustCompile
(
"https?://"
)
imgReg
=
regexp
.
MustCompile
(
`!\[([^]]+)\]\([^)]+\)`
)
)
// PostLede attempts to extract the first thought of the given post, generally
// contained within the first line or sentence of text.
func
PostLede
(
t
string
,
includePunc
bool
)
string
{
// Adjust where we truncate if we want to include punctuation
iAdj
:=
0
if
includePunc
{
iAdj
=
1
}
// Find lede within first line of text
nl
:=
strings
.
IndexRune
(
t
,
'\n'
)
if
nl
>
-
1
{
t
=
t
[:
nl
]
}
// Strip certain HTML tags
t
=
titleElementReg
.
ReplaceAllString
(
t
,
""
)
// Strip URL protocols
t
=
urlReg
.
ReplaceAllString
(
t
,
""
)
// Strip image URL, leaving only alt text
t
=
imgReg
.
ReplaceAllString
(
t
,
" $1 "
)
// Find lede within first sentence
punc
:=
strings
.
Index
(
t
,
". "
)
if
punc
>
-
1
{
t
=
t
[:
punc
+
iAdj
]
}
punc
=
stringmanip
.
IndexRune
(
t
,
'。'
)
if
punc
>
-
1
{
c
:=
[]
rune
(
t
)
t
=
string
(
c
[:
punc
+
iAdj
])
}
return
t
}
// TruncToWord truncates the given text to the provided limit.
func
TruncToWord
(
s
string
,
l
int
)
(
string
,
bool
)
{
truncated
:=
false
c
:=
[]
rune
(
s
)
if
len
(
c
)
>
l
{
truncated
=
true
s
=
string
(
c
[:
l
])
spaceIdx
:=
strings
.
LastIndexByte
(
s
,
' '
)
if
spaceIdx
>
-
1
{
s
=
s
[:
spaceIdx
]
}
}
return
s
,
truncated
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Jul 17, 8:05 PM (18 h, 56 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3324349
Attached To
rWF WriteFreely
Event Timeline
Log In to Comment