当我们缩短/截断文本内容时,我们通常只是在特定的字符索引处截断它。这在HTML中已经很复杂了,但是我想使用不同的度量来截断我的HTML内容(使用内容可编辑的div生成):
N,它将用作截断起始点限制N字符长(仅文本;不计数标记);如果不是,则只返回整个内容。N-X到N+X字符位置(仅文本)并搜索块节点的末端;X是预定义的偏移量值,很可能是关于N/5到N/4的;N的节点。N的索引并在该位置截断。我的内容可编辑生成的内容可能包括段落(带换行)、预先格式化的代码块、块引号、有序和无序列表、标题、粗体和斜体(它们是内联节点,不应该在截断过程中计算)等等。当然,最后的实现将定义哪些元素是可能的截断候选项。头,即使它们是块HTML元素,也不会被计算为截断点,因为我们不希望被守恒的标题。段落,列出个别项目,完整的有序和无序列表,块引号,预先格式化的块,空元素等都是好的。标题和所有内联块元素都不是。
示例
让我们把这个非常堆栈溢出的问题作为HTML内容的一个例子,我想截断它。让我们将截断限制设置为1000,偏移量为250个字符(1/4)。
这个DotNetFiddle显示了这个问题的文本,同时在其中添加了限制标记(|MIN|表示字符750,|LIMIT|表示字符1000,|MAX|表示字符1250)。
从示例中可以看出,两个块节点之间最接近的截断边界(到字符1000 )是在</OL>和P (我的内容可编辑生成.)之间。这意味着我的HTML应该在这两个标记之间被截断,这将导致稍微小于1000个字符的长内容文本,但是保持截断内容的意义,因为它不会只是在某个文本段落中间的某个地方截断。
我希望这能解释为什么事情应该与这个算法相关。
问题所在
我在这里看到的第一个问题是,我正在处理像HTML这样的嵌套结构。我还必须检测不同的元素(只有块元素,没有内联元素)。最后但并非最不重要的一点是,我只需计算字符串中的某些字符,而忽略那些属于标记的字符。
可能的解决办法
N最近的新行,然后再转换回HTML。二思
应该如何处理这种截断算法?我的头脑似乎太累了,无法达成共识(或解决方案)。
发布于 2015-06-30 09:57:55
下面是一些可以截断内部文本的示例代码。它使用了InnerText属性和CloneNode方法的递归功能。
public static HtmlNode TruncateInnerText(HtmlNode node, int length)
{
if (node == null)
throw new ArgumentNullException("node");
// nothing to do?
if (node.InnerText.Length < length)
return node;
HtmlNode clone = node.CloneNode(false);
TruncateInnerText(node, clone, clone, length);
return clone;
}
private static void TruncateInnerText(HtmlNode source, HtmlNode root, HtmlNode current, int length)
{
HtmlNode childClone;
foreach (HtmlNode child in source.ChildNodes)
{
// is expected size is ok?
int expectedSize = child.InnerText.Length + root.InnerText.Length;
if (expectedSize <= length)
{
// yes, just clone the whole hierarchy
childClone = child.CloneNode(true);
current.ChildNodes.Add(childClone);
continue;
}
// is it a text node? then crop it
HtmlTextNode text = child as HtmlTextNode;
if (text != null)
{
int remove = expectedSize - length;
childClone = root.OwnerDocument.CreateTextNode(text.InnerText.Substring(0, text.InnerText.Length - remove));
current.ChildNodes.Add(childClone);
return;
}
// it's not a text node, shallow clone and dive in
childClone = child.CloneNode(false);
current.ChildNodes.Add(childClone);
TruncateInnerText(child, root, childClone, length);
}
}以及一个示例C#控制台应用程序,它将把这个问题作为一个例子,并将其截断为500个字符。
class Program
{
static void Main(string[] args)
{
var web = new HtmlWeb();
var doc = web.Load("http://stackoverflow.com/questions/30926684/truncating-html-content-at-the-end-of-text-blocks-block-elements");
var post = doc.DocumentNode.SelectSingleNode("//td[@class='postcell']//div[@class='post-text']");
var truncated = TruncateInnerText(post, 500);
Console.WriteLine(truncated.OuterHtml);
Console.WriteLine("Size: " + truncated.InnerText.Length);
}
}运行它时,它应该显示如下:
<div class="post-text" itemprop="text">
<p>Mainly when we shorten/truncate textual content we usually just truncate it at specific character index. That's already complicated in HTML anyway, but I want to truncate my HTML content (generated using content-editable <code>div</code>) using different measures:</p>
<ol>
<li>I would define character index <code>N</code> that will serve as truncating startpoint <em>limit</em></li>
<li>Algorithm will check whether content is at least <code>N</code> characters long (text only; not counting tags); if it's not it will just return the whole content</li>
<li>It would then</li></ol></div>
Size: 500注意:我没有在字界截断,只是在字符边界处截断,而且不,我的评论中的建议一点也不符合:-)
发布于 2015-06-29 17:45:43
private void RemoveEmpty(HtmlNode node){
var parent = node.Parent;
node.Remove();
if(parent==null)
return;
// remove parent if it is empty
if(!parent.DescendantNodes.Any()){
RemoveEmpty(parent);
}
}
private void Truncate(DocumentNode root, int maxLimit){
var n = 0;
HtmlTextNode lastNode = null;
foreach(var node in root.DescendantNodes
.OfType<HtmlTextNode>().ToArray()){
var length = node.Text.Length;
n+= length;
if(n + length >= maxLimit){
RemoveEmpty(node);
}
}
}
// you are left with only nodes that add up to your max limit characters.发布于 2015-06-20 08:18:09
我将运行整个DOM树,并继续计算出现的文本字符数。每当我达到极限(N),我将删除该文本节点的额外字符,从那里我将只删除所有的文本节点。
我相信这是一个安全的方法,以保持所有的HTML+CSS结构,而只保留N个字符。
https://stackoverflow.com/questions/30926684
复制相似问题