quinta-feira, 28 de janeiro de 2016

Bash script to convert from HTML entities to characters - Stack Overflow

Bash script to convert from HTML entities to characters - Stack Overflow



I'm looking for a way to turn this:
hello < world
to this:
hello < world
I could use sed with a bunch of substitutions, but isn't there a tool that will do that for me in one go?
shareimprove this question
Try recode:
$ echo '&lt;' |recode html..ascii
<
shareimprove this answer
1 
link seems dead now – uglycoyote Apr 8 '15 at 16:44
1 
@uglycoyote Unfortunately. The Debian package might be a good alternative source:packages.debian.org/en/sid/recode. There is also a copy at Github: github.com/pinard/Recode – ceving Apr 13 '15 at 9:25 
With perl:
cat foo.html | perl -MHTML::Entities -e 'while(<>) {print decode_entities($_);}'
With php from the command line:
cat foo.html | php -r 'while(($line=fgets(STDIN)) !== FALSE) echo html_entity_decode($line, ENT_QUOTES|ENT_HTML401);'
shareimprove this answer
1 
The PHP one is not working for certain characters such as &nbsp; – Romain Paulus Dec 20 '13 at 5:13
3 
Shorter Perl version: perl -MHTML::Entities -pe 'decode_entities($_);' – RobEarl Aug 7 '14 at 8:48
1 
I'll give you an upvote if you remove the useless use of cat (en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat) :-) – 0x89 Aug 19 '14 at 9:10
   
Use perl -C -MHTML::Entities -pe 'decode_entities($_);' < foo.html to output UTF-8 (see this question) – tricasse Oct 2 '15 at 9:15
An alternative is to pipe through a web browser -- such as:
echo '&#33;' | w3m -dump -T text/html
This worked great for me in cygwin, where downloading and installing distributions are difficult.
This answer was found here
shareimprove this answer
Using xmlstarlet:
echo 'hello &lt; world' | xmlstarlet unesc
shareimprove this answer
2 
Note that this does not work for hexa entities like &#x3a;. – v6ak Aug 13 '13 at 21:00

domingo, 4 de janeiro de 2015

http://www.cyberciti.biz/open-source/html-to-pdf-freeware-linux-osx-windows-software/


Convert HTML Page To a PDF Using Open Source Tool [ Linux / OS X / Windows ]

by  on OCTOBER 3, 2012 · 16 COMMENTS· LAST UPDATED OCTOBER 4, 2012
Do you need a simple open source cross-platform command line tool that converts web pages and HTML to a PDF file? Look no further, try wkhtmltopdf.

From the project home page:
Simple shell utility to convert html to pdf using the webkit rendering engine, and qt. Searching the web, I have found several command line tools that allow you to convert a HTML-document to a PDF-document, however they all seem to use their own, and rather incomplete rendering engine, resulting in poor quality. Recently QT 4.4 was released with a WebKit widget (WebKit is the engine of Apples Safari, which is a fork of the KDE KHtml), and making a good tool became very easy.

Software features

  1. Cross platform.
  2. Open source.
  3. Convert any web pages into PDF documents using webkit.
  4. You can add headers and footers.
  5. TOC generation.
  6. Batch mode conversions.
  7. Can run on Linux server with an XServer (the X11 client libs must be installed).
  8. Can be directly used by PHP or Python via bindings to libwkhtmltox.

A note about Debian / Ubuntu Linux user

You can install wkhtmltopdf using apt-get command:
$ sudo apt-get install wkhtmltopdf
$ sudo ln -s /usr/bin/wkhtmltopdf /usr/local/bin/html2pdf

Sample outputs:
[sudo] password for vivek:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  wkhtmltopdf
0 upgraded, 1 newly installed, 0 to remove and 10 not upgraded.
Need to get 116 kB of archives.
After this operation, 303 kB of additional disk space will be used.
Get:1 http://debian.osuosl.org/debian/ squeeze/main wkhtmltopdf amd64 0.9.9-1 [116 kB]
Fetched 116 kB in 2s (49.4 kB/s)
Selecting previously deselected package wkhtmltopdf.
(Reading database ... 274164 files and directories currently installed.)
Unpacking wkhtmltopdf (from .../wkhtmltopdf_0.9.9-1_amd64.deb) ...
Processing triggers for man-db ...
Setting up wkhtmltopdf (0.9.9-1) ...

Download wkhtmltopdf

Visit this page to grab wkhtmltopdf for Linux / MS-Windows / Apple Mac OS X. You can also use the wget command as follows:
$ wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2
Sample outputs:
Resolving wkhtmltopdf.googlecode.com... 74.125.135.82, 2404:6800:4001:c01::52
Connecting to wkhtmltopdf.googlecode.com|74.125.135.82|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11175276 (11M) [application/octet-stream]
Saving to: `wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2'
 
100%[======================================>] 1,11,75,276  480K/s   in 23s
 
2012-10-04 01:21:43 (477 KB/s) - `wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2' saved [11175276/11175276]
 

Install wkhtmltopdf under Linux

Type the following tar command to extract files:
$ tar xvf wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2
Sample outputs:
wkhtmltopdf-amd64
Install the same in your private ~/bin/ directory or in /usr/local/bin directory:
$ mv wkhtmltopdf-amd64 ~/bin/
ln -s ~/bin/wkhtmltopdf-amd64 ~/bin/html2pdf

OR
$ sudo mv wkhtmltopdf-amd64 /usr/local/bin/
ln -s /usr/local/bin/wkhtmltopdf-amd64 /usr/local/bin/html2pdf

How do I use wkhtmltopdf?

The syntax is as follows:
 
html2pdf http://www.cyberciti.biz/path/to/url.html output.pdf
html2pdf http://www.cyberciti.biz/blog/print/url-slut.html output.pdf
html2pdf -option1  -option2 http://www.cyberciti.biz/blog/print/url-slut.html output.pdf
 

Example: Simple html to pdf file

In this example, convert out bash for loop page to a pdf file:
$ html2pdf http://www.cyberciti.biz/faq/bash-for-loop/print/ /tmp/bash.for.loop.pdf
Sample outputs:
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
To view generated pdf file click here. Verify pdf file, enter:
$ file /tmp/bash.for.loop.pdf
Sample outputs:
/tmp/bash.for.loop.pdf: PDF document, version 1.4
Use the pdfinfo command to prints the contents of the 'Info' dictionary (plus some other useful information) from a Portable Document Format (PDF) file:
$ pdfinfo /tmp/bash.for.loop.pdf
Sample outputs:
Title:          Frequently Asked Questions About Linux / UNIX » Bash For Loop Examples » Print
Creator:
Producer:       wkhtmltopdf
CreationDate:   Thu Oct  4 01:29:33 2012
Tagged:         no
Pages:          4
Encrypted:      no
Page size:      595 x 842 pts (A4)
File size:      98792 bytes
Optimized:      no
PDF version:    1.4

Grayscale pdf

The following PDF will be generated in grayscale:
$ html2pdf -g http://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf

Set orientation to Landscape or Portrait

Use the following syntax:
$ html2pdf -O Landscape http://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf
Where,
  • -O Landscape|Portrait. The default is Portrait.

How do I set page size?

Use the following syntax:
$ html2pdf -S SIZE http://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf
Where,
  • -s Size : Set paper size to: A4, Letter, etc. (default A4)

How do I generate table of content?

A table of content can be added to the document by adding a toc the command line option. For example:
$ html2pdf toc http://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf
Sample outputs:
Linux / Unix HTML to PDF File Command Line Option
Fig.01: wkhtmltopdf in action

Please note that the table of content is generated based on the H tags in the input documents.

How do I see all available options?

You can see a list of commonly used options, enter:
$ wkhtmltopdf --help
OR see all available options i.e. display more extensive help, detailing less common command switches, run:
$ wkhtmltopdf -H | less
REFERENCES:

domingo, 24 de agosto de 2014

C# Sort Dictionary

http://www.dotnetperls.com/sort-dictionary



C# Sort Dictionary

Sorted letters: A to Z



Dictionary has no Sort method. If we need to loop through the Dictionary contents in sorted order, we must separately acquire the elements and sort them. This is done with the Keys and Values properties and a List instance.

DictionaryList

Example

Dictionary



This example solves the problem by using the Keys property on the Dictionary instance. Then it uses the ToList extension method and the Sort instance method. An example Dictionary is created and populated with the Add method.



Next:The ToList and Sort methods are used on the Keys. The resulting List is looped through using the foreach-loop construct.

ToListForeach



Note:The var implicit typed keyword is used throughout to reduce syntactic redundancy.



Program that sorts keys in Dictionary: C#



using System;

using System.Collections.Generic;

using System.Linq;



class Program

{

    static void Main()

    {

// Create dictionary and add five keys and values.

var dictionary = new Dictionary<string, int>();

dictionary.Add("car", 2);

dictionary.Add("apple", 1);

dictionary.Add("zebra", 0);

dictionary.Add("mouse", 5);

dictionary.Add("year", 3);



// Acquire keys and sort them.

var list = dictionary.Keys.ToList();

list.Sort();



// Loop through keys.

foreach (var key in list)

{

   Console.WriteLine("{0}: {1}", key, dictionary[key]);

}

    }

}



Output



apple: 1

car: 2

mouse: 5

year: 3

zebra: 0



Example 2

KeyValuePair: Key and Value properties



Next we show how to sort the values in a Dictionary. This program adds keys to a Dictionary and then sorts them by their values. Dictionary instances are not initially sorted in any way. We use the orderby keyword in a query statement.

OrderBy Clause



Program that sorts Dictionary: C#



using System;

using System.Collections.Generic;

using System.Linq;



class Program

{

    static void Main()

    {

// Example dictionary.

var dictionary = new Dictionary<string, int>(5);

dictionary.Add("cat", 1);

dictionary.Add("dog", 0);

dictionary.Add("mouse", 5);

dictionary.Add("eel", 3);

dictionary.Add("programmer", 2);



// Order by values.

// ... Use LINQ to specify sorting by value.

var items = from pair in dictionary

   orderby pair.Value ascending

   select pair;



// Display results.

foreach (KeyValuePair<string, int> pair in items)

{

   Console.WriteLine("{0}: {1}", pair.Key, pair.Value);

}



// Reverse sort.

// ... Can be looped over in the same way as above.

items = from pair in dictionary

orderby pair.Value descending

select pair;

    }

}



Output



dog: 0

cat: 1

programmer: 2

eel: 3

mouse: 5



Steps



First, it declares an example Dictionary. It contains keys in an arbitrary order. The Dictionary keys are strings. Its values are ints. We will reorder the values to go from lowest to highest.



It uses a LINQ query with the var keyword and accesses the KeyValuePairs from the Dictionary. It finally displays results, using foreach to iterate through and display each KeyValuePair. No additional lookups are required.

LINQVarKeyValuePair



Note:Thanks to Jon Senchyna for improving the query expression. It is more efficient to use the collection of KeyValuePairs.

OrderBy

Lambda expression syntax



Another option is the OrderBy extension method in System.Linq. This approach compiles to the same code as the orderby query expression. But the syntax is shorter. It requires only one lambda expression and method call.

Lambda



Program that uses OrderBy method: C#



using System;

using System.Collections.Generic;

using System.Linq;



class Program

{

    static void Main()

    {

var items = new Dictionary<int, int>();

items.Add(-1, 0);

items.Add(0, 1);

items.Add(-2, 0);

items.Add(3, 1);



// Use OrderBy method.

foreach (var item in items.OrderBy(i => i.Key))

{

   Console.WriteLine(item);

}

    }

}



Output



[-2, 0]

[-1, 0]

[0, 1]

[3, 1]



Note:Thanks to Even Holen for writing in with an example of the shorter OrderBy syntax form.

Discussion

Warning: exclamation mark



You will likely add more logic to the solution here for your project.

The above console program could,

with certain changes,

raise a KeyNotFoundException. You will want to trap those errors with exception handling\u2014try and catch.

TryCatch



It is possible and easy to sort in the opposite direction. Simply replace the keyword ascending with descending. When you omit the direction keyword entirely, it will use ascending. You can find more information on descending.

Descending



Descending sort



var items = from pair in dictionary

   orderby pair.Value descending

   select pair;



Example output



mouse: 5

eel: 3

programmer: 2

cat: 1

dog: 0



Question and answer



Other methods I found involve more steps, more lines of code, or delegates and anonymous functions. There is nothing wrong with those methods, although they vary in efficiency. You may prefer this sort of home-grown solution.



Sort string values. Sorting strings would work just as well. What the runtime is doing is using the interface implementations of the types. Its syntax is the same for any type that List.Sort() could sort.

IComparable

Summary



As a hashtable, Dictionary is optimized for fast lookups, not for specific looping mechanisms. So while Dictionary is invaluable for lookup-heavy programs, it hinders programs that demand certain enumeration patterns\u2014such as sorted keys.



C#: Sort


What's a busness object

http://docs.imis.com/15.2.0/index.htm?turl=whatisabusinessobject2.htm



What is a business Object?

A general definition, from the Object Management Group:
A business object is a representation of a thing active in the business domain, including at least its business name and definition, attributes, behavior, relationships and constraints. A business object may represent, for example, a person, place or concept. The representation may be in a natural language, a modeling language, or a programming language.
An iMIS-specific definition, in terms of the Business Object Designer:
A business object is an iMIS system construct representing the data elements and business rules of a business concept like a contact; it is implemented using a combination of business rules, a schema definition that describes the data structures of the object’s properties, and a database view.
Elements of a Business Object
These are the basic elements of an iMIS business object.
■    Properties – attributes of the object (typically map to database columns). For example, Contact.LastName
■    Property constraints – logic that is executed whenever a property value changes. If constraint logic fails, the property value is not changed.  For example, LastName cannot be blank
■    Object constraints – logic that is executed whenever an object is created, modified, or deleted. If constraint logic fails, the attempted action is not committed. For example, If the contact gender is female, then the prefix cannot be Mr.
■    Actions – logic that is executed whenever an object is created, modified, or deleted. Actions do not pass or fail (unlike constraints), they just run. For example, When a company contact is set to inactive, set all company employees to inactive


■    Branches – used to provide inheritance-like functionality for similar objects – i.e., objects similar enough to be represented within a single object definition.